pythonsocketsreverse-dnsgethostbyaddr

Python Sockets: gethostbyaddr : Reverse DNS Lookup Failure


I've been having a problem with getting the host name while using socket.gethostbyaddr(ip_addr) on specific sites.

I will not go into detail about which site this is not working for.

so getting the host by name works fine for every site I've tried so far , but then when I try to get the site name from I get an error say ing host not found.

A fix or an alternative would be nice for this to have complete data. If there is no fix I can merely leave out the host name. no biggie. Thanks for the help.

# not full code
hostip = socket.gethostbyname(hostname)
print socket.gethostbyaddr(hostip)

Error: socket.herror: [Errno 11004] host not found

Solution

  • Not every IP address has reverse DNS. Sometimes this is on purpose, sometimes it's because you're looking at an internal address and there's no need for it inside the network so it wasn't worth setting up, sometimes someone just screwed up.

    Why would anyone do this on purpose? Most commonly, because multiple domain names map to the same IP address.

    For example, a shared hosting site might map sites for three of its customers, www.foo.com, www.bar.com, and www.baz.com, all to 1.2.3.4. HTTP gives you the requested host name in a Host: header, so it can figure out which site your browser wanted to go. But outside of HTTP (or some other higher-level protocol), there's no way to figure out which of the three names you meant with 1.2.3.4. So, there's nothing they can provide that would be useful to you. There may also be a name like shared_1234.hostingcompany.com which is useful to their own IT people, in which case they might provide that, but otherwise, they won't bother with any reverse DNS.