socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error

Nick Dokos nicholas.dokos at hp.com
Mon Jan 9 18:32:44 EST 2012


Chris Angelico <rosuav at gmail.com> wrote:

> On Tue, Jan 10, 2012 at 9:35 AM, Dominic Binks <dbinks at codeaurora.org> wrote:
> > The address database can have multiple names for the same IP address so
> > gethostbyaddr returns a list - [0] being the first item in the list (and
> > usually considered the canonical name)
> 
> Point to note: The inverse operation, getting IP addresses from names,
> quite frequently returns multiple results - and in that case, there's
> no "canonical name". You'll still usually want to take the zeroth
> (first) item in the list, but often the list will change in order.
> Try, for instance:
> 
> $ dig +short www.google.com
> 
> which uses DNS rotation as a form of load-balancing.
> 
> In any case, you normally just take the first entry.
> 

On both 2.6.6 and 2.7.1, the doc says: 

,----
| >>> help(socket.gethostbyaddr)
| Help on built-in function gethostbyaddr in module _socket:
| 
| gethostbyaddr(...)
|     gethostbyaddr(host) -> (name, aliaslist, addresslist)
|     
|     Return the true host name, a list of aliases, and a list of IP addresses,
|     for a host.  The host argument is a string giving a host name or IP number.
`----

so socket.gethostbyaddr(os.environ['REMOTE_ADDR'])[0] is the 0th element
of the tuple, i.e. the *name* that is returned. So (unless I'm confused
which is always a distinct possibility) this has nothing to do with
load-balancing or multiple addresses: it's the equivalent of 

  dig +short -x <addr>

Nick




More information about the Python-list mailing list