Django get client's domain name/host name

2019-04-14 15:08发布

What is the easiest way to obtain the user's host/domain name, if available? Or is there a function to lookup the IP address of the user to see if it is bound to a named address? (i.e. gethostbyaddr() in PHP)

HttpRequest.get_host() only returns the IP address of the user.

3条回答
We Are One
2楼-- · 2019-04-14 15:40

You can't rely on it, but you can try the request.META['REMOTE_HOST'] or request.META['HTTP_HOST'] from the META dictionary of the request :

A standard Python dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples:

- REMOTE_HOST -- The hostname of the client.
- HTTP_HOST -- The HTTP Host header sent by the client.
查看更多
做个烂人
3楼-- · 2019-04-14 15:49

You can just print HttpRequest.META and find what you want and I think req.META['HTTP_ORIGIN'] is the thing you need, It's the same as the browser address bar value.

查看更多
仙女界的扛把子
4楼-- · 2019-04-14 15:56
登录 后发表回答