Python Sockets: gethostbyaddr : Reverse DNS Lookup Failure

我只是一个虾纸丫 提交于 2019-12-12 23:42:12

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/19169412/python-sockets-gethostbyaddr-reverse-dns-lookup-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!