我有一个小问题,我似乎无法解决问题。我正在尝试查询子域的序列号。我一直没有收到任何答案错误,但它可以在根域上正常工作。如果我向您展示会更容易:
import socket, dns.resolver
host = "google.com"
querytype = "SOA"
cachingserverslist = {'server1': '4.1.1.1', 'server2': '4.2.2.2'}
for cachingservername, cachingserver in sorted(cachingserverslist.iteritems()) :
query = dns.resolver.Resolver()
query.nameservers=[socket.gethostbyname(cachingserver)]
query.Timeout = 2.0
for a in query.query( host , querytype ) :
print a.serial
这给了我预期的结果。我不明白的是,当我将主机变量更改为任何子域或 www 时,它会出错而没有答案。这是一个 ipython 会话,它显示了我的意思:
In [1]: import socket, dns.resolver
In [2]: host = "google.com"
In [3]: querytype = "SOA"
In [4]: cachingserverslist = {'server1': '4.1.1.1', 'server2': '4.2.2.2'}
In [5]: for cachingservername, cachingserver in sorted(cachingserverslist.iteritems()) :
...: query = dns.resolver.Resolver()
...: query.nameservers=[socket.gethostbyname(cachingserver)]
...: query.Timeout = 2.0
...:
In [6]: for a in query.query( host , querytype ) :
...: print a.serial
...:
2011121901
In [7]:
In [8]: host = "www.google.com"
In [9]: for a in query.query( host , querytype ) :
print a.serial
....:
....:
---------------------------------------------------------------------------
NoAnswer Traceback (most recent call last)
/var/www/pydns/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/dns/resolver.pyc in query(self, qname, rdtype, rdclass, tcp, source, raise_on_no_answer)
707 raise NXDOMAIN
708 answer = Answer(qname, rdtype, rdclass, response,
--> 709 raise_on_no_answer)
710 if self.cache:
711 self.cache.put((qname, rdtype, rdclass), answer)
/usr/local/lib/python2.6/dist-packages/dns/resolver.pyc in __init__(self, qname, rdtype, rdclass, response, raise_on_no_answer)
127 except KeyError:
128 if raise_on_no_answer:
--> 129 raise NoAnswer
130 if raise_on_no_answer:
131 raise NoAnswer
NoAnswer:
任何见解将不胜感激。谢谢。