Python lookup hostname from IP with 1 second timeout


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!

Asked By: ensnare



>>> import socket
>>> socket.gethostbyaddr("")
('', [''], [''])

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

Answered By: ChristopheD

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

# => (hostname, alias-list, IP)

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.

Answered By: adamse
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.