socket.gethostbyaddr() wrongly returning a DNS CNAME as the 'primary' hostname

Ned Deily nad at
Thu Apr 28 21:34:45 CEST 2011

In article 
<850c85c0-be6e-4eec-b6b1-5c79e1716f6d at
>, cjblaine <cjblaine at> wrote:
> Python 2.7.1 (perhaps others)
> I believe this is a bug.  Comments?
> Docs state: Return a triple (hostname, aliaslist, ipaddrlist)
> where hostname is the primary host name responding to the
> given ip_address, aliaslist is a (possibly empty) list of
> alternative host names  for the same address, and ipaddrlist
> is a list of IPv4/v6 addresses for the same interface on the
> same host (most likely containing only a single address).
> is a CNAME record for
> has IP address xx.xx.xx.xx
> import socket
> socket.gethostbyaddr('xx.xx.xx.xx')
> returns:
>     ('', [''], ['xx.xx.xx.xx'])

socket.gethostbyaddr() is pretty much just a transparent wrapper around 
the platform's C library function of the same name.  See if a simple C 
program with a similar call to gethostbyaddr produces the same results.  
The code in question for Python 2.7 is here:

 Ned Deily,
 nad at

More information about the Python-list mailing list