Python lookup hostname from IP with 1 second timeout Python lookup hostname from IP with 1 second timeout python python

Python lookup hostname from IP with 1 second timeout


>>> import socket>>> socket.gethostbyaddr("69.59.196.211")('stackoverflow.com', ['211.196.59.69.in-addr.arpa'], ['69.59.196.211'])

For implementing the timeout on the function, this stackoverflow thread has answers on that.


What you're trying to accomplish is called Reverse DNS lookup.

socket.gethostbyaddr("IP") # => (hostname, alias-list, IP)

http://docs.python.org/library/socket.html?highlight=gethostbyaddr#socket.gethostbyaddr

However, for the timeout part I have read about people running into problems with this. I would check out PyDNS or this solution for more advanced treatment.