使用不同的名称服务器查询

query with different name server

我要更改域的默认名称服务器,然后获取其 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.

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