用不同的名称服务器查询

时间:2017-01-09 00:44:11

标签: python dns dnspython

我要更改域的默认名称服务器,然后获取其A记录!我尝试使用以下代码,但问题是,当我使用IP地址作为名称服务器时,每件事情都有效但当我使用名称服务器名称如“ns1.google.com”时,我无法运行代码并获得异常。有什么想法?(我使用了这里的代码:Set specific DNS server using dns.resolver (pythondns)

def NsLookupSpecificNS(domain):
    my_resolver = dns.resolver.Resolver()

    # 8.8.8.8 is Google's public DNS server
    my_resolver.nameservers = ['216.239.38.10']

    answer = my_resolver.query('google.com', 'A')
    try:
        for rdata in answer:
            print rdata
    except dns.resolver.NoAnswer:
            pass

我已经厌倦了以下事情:

def NsLookupSpecificNS(domain):
    my_resolver = dns.resolver.Resolver()

    # 8.8.8.8 is Google's public DNS server
    my_resolver.nameservers = ['ns1.google.com']

    answer = my_resolver.query('google.com', 'A')
    try:
        for rdata in answer:
            print rdata
    except dns.resolver.NoAnswer:
            pass

这就是我得到的:

Traceback (most recent call last):
  File "C:\Users\My Documents\LiClipse Workspace\DNS\Lookup.py", line 49, in <module>
    NsLookupSpecificNS('google.com')
  File "C:\Users\My Documents\LiClipse Workspace\DNS\Lookup.py", line 33, in NsLookupSpecificNS
    answer = my_resolver.query('google.com', 'A')
  File "C:\Python27\lib\site-packages\dns\resolver.py", line 962, in query
    source_port=source_port)
  File "C:\Python27\lib\site-packages\dns\query.py", line 242, in udp
    if _addresses_equal(af, from_address, destination) or \
  File "C:\Python27\lib\site-packages\dns\query.py", line 169, in _addresses_equal
    n2 = dns.inet.inet_pton(af, a2[0])
  File "C:\Python27\lib\site-packages\dns\inet.py", line 51, in inet_pton
    return dns.ipv4.inet_aton(text)
  File "C:\Python27\lib\site-packages\dns\ipv4.py", line 48, in inet_aton
    raise dns.exception.SyntaxError
dns.exception.SyntaxError: Text input is malformed.

1 个答案:

答案 0 :(得分:3)

在dnspython中,名称服务器实例变量获取了一个IP地址列表而不是域名列表,这就是您收到该错误的原因。您需要单独查询ns1.google.com的地址,并将该地址(或地址)用于后续查询。当然,这引出了你在哪里进行第一次查询的问题。为此,您可以尝试使用系统解析器。