2

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 Answers3

5

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.
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • 1
    +1 "You can't rely on it". That is very correct. Network configurations can mask or completely hide the IP address of the user's machine. Many places do it deliberately for security reasons, others do it due to NAT. Trying to do a reverse lookup will give some pretty bogus results as a result. – the Tin Man Oct 26 '12 at 20:54
  • How is this distinct from `HttpRequest.get_host()`? What address is that one reporting? – Brian Cain Oct 26 '12 at 21:03
  • **HttpRequest.get_host()** gives you **requested** hostname. Hostname of your own website. – tonino.j Nov 29 '12 at 18:04
4

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.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
user6689187
  • 101
  • 3
3

You can use socket.gethostbyaddr()

Brian Cain
  • 14,403
  • 3
  • 50
  • 88