当我使用 dnspython 查询权威名称服务器以获取 NS 记录时,它会引发 NoAnswer 异常,尽管我的数据包捕获显示已收到正确的响应。
示例:向 j.gtld-servers.net (192.48.79.30) 询问 stackoverflow.com 的 NS 记录
>>> import dns.resolver
>>> r = dns.resolver.Resolver()
>>> r.nameservers = ['192.48.79.30']
>>> for answer in r.query('stackoverflow.com', 'NS'):
... print answer.to_text()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/dns/resolver.py", line 905, in query
raise_on_no_answer)
File "/usr/local/lib/python2.7/dist-packages/dns/resolver.py", line 142, in __init__
raise NoAnswer
dns.resolver.NoAnswer
>>>
Wireshark 表明,事实上,答案确实回来了:
Frame 2: 157 bytes on wire (1256 bits), 157 bytes captured (1256 bits)
Ethernet II, Src: xxxxxxxxxxxxxx (xx:xx:xx:xx:xx:xx), Dst: xxxxxxxxxxxxx (xx:xx:xx:xx:xx:xx)
Internet Protocol Version 4, Src: 192.48.79.30 (192.48.79.30), Dst: xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)
User Datagram Protocol, Src Port: domain (53), Dst Port: 55100 (55100)
Domain Name System (response)
[Request In: 1]
[Time: 0.278279000 seconds]
Transaction ID: 0xb6f5
Flags: 0x8100 (Standard query response, No error)
Questions: 1
Answer RRs: 0
Authority RRs: 2
Additional RRs: 2
Queries
Authoritative nameservers
stackoverflow.com: type NS, class IN, ns ns1.serverfault.com
stackoverflow.com: type NS, class IN, ns ns2.serverfault.com
Additional records
ns1.serverfault.com: type A, class IN, addr 198.252.206.80
ns2.serverfault.com: type A, class IN, addr 198.252.206.81
查询dig @192.48.79.30 stackoverflow.com ns
成功并提供与上面数据包捕获中显示的相同的答案。
有趣的是,host -t NS stackoverflow.com 192.48.79.30
响应“stackoverflow.com 没有 NS 记录”失败,在这种情况下,数据包捕获再次显示正在接收响应。
为什么 dnspython 不能正确处理对此查询的响应?