Python lookup hostname from IP with 1 second timeout

ensnare picture ensnare · Apr 4, 2010 · Viewed 106.2k times · Source

How can I look up a hostname given an IP address? Furthermore, how can I specify a timeout in case no such reverse DNS entry exists? Trying to keep things as fast as possible. Or is there a better way? Thank you!

Answer

ChristopheD picture ChristopheD · Apr 4, 2010
>>> 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.