在Python中反向DNS查找 [英] Reverse DNS lookup in Python
问题描述
如果我的IP地址是2001:4860:4860 :: 8888
If I have an IP address like 2001:4860:4860::8888
如何获取格式为foo.ip6.arpa的完全限定域名?
How can I get the fully qualified domain in the format foo.ip6.arpa ?
到目前为止,两种解决方案都给我google-public-dns-a.google.com-也许反向DNS是错误的名称.为此,我希望输出为8.8.8.8.0 ... etc.ip6.arpa
Both the solutions so far give me google-public-dns-a.google.com - maybe Reverse DNS was the wrong name. For this I'd expect the output to be something like 8.8.8.8.0...etc.ip6.arpa
推荐答案
IPy 提供您想要的方法:
>>> from IPy import IP
>>> ip = IP('127.0.0.1')
>>> ip.reverseName()
'1.0.0.127.in-addr.arpa.'
适用于IPv4和IPv6,尽管原始IPy对于IPv6有一些错误.我在 https://github.com/steffann/python-ipy ,只要未将这些修订合并到原始代码中就可以使用.
Works for both IPv4 and IPv6, although the original IPy has a few bugs for IPv6. I created a fork with some extensions and fixes at https://github.com/steffann/python-ipy which you can use as long as the fixes haven't been merged back into the original code.
您当然也可以使用内置的 socket
模块:
You can of course also use the built-in socket
module:
>>> import socket
>>> socket.getnameinfo(('2001:4860:4860::8888', 0), 0)
('google-public-dns-a.google.com', '0')
>>> socket.getnameinfo(('127.0.0.1', 0), 0)
('localhost', '0')
您需要提供一个host + port元组,但是您可以为该端口提供 0
,然后您将获得主机名.
You need to provide a host+port tuple, but you can provide 0
for the port, and you'll get the hostname back.
这篇关于在Python中反向DNS查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!