[英]Python lookup hostname from IP with 1 second timeout
How can I look up a hostname given an IP address? 如何在给定IP地址的情况下查找主机名? Furthermore, how can I specify a timeout in case no such reverse DNS entry exists? 此外,如果不存在此类反向DNS条目,如何指定超时? Trying to keep things as fast as possible. 尽量保持事物的速度。 Or is there a better way? 或者,还有更好的方法? Thank you! 谢谢!
>>> 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. 为了在函数上实现超时, 这个stackoverflow线程有答案。
What you're trying to accomplish is called Reverse DNS lookup. 您要完成的任务称为反向DNS查找。
socket.gethostbyaddr("IP")
# => (hostname, alias-list, IP)
http://docs.python.org/library/socket.html?highlight=gethostbyaddr#socket.gethostbyaddr 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. 我会查看PyDNS或此解决方案以获得更高级的治疗。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.