5

我正在使用dnspython获取“A”记录并返回结果(给定域的 IP 地址)。

我有这个简单的测试 python 脚本:

import dns.resolver

def resolveDNS():
    domain = "google.com" 
    resolver = dns.resolver.Resolver(); 
    answer = resolver.query(domain , "A")
    return answer

resultDNS = resolveDNS()
print resultDNS

但是,输出是:

<dns.resolver.Answer object at 0x0000000004F56C50>

我需要将结果作为字符串。如果是字符串数组,怎么返回呢?

4

2 回答 2

6

The answer(s) you get is actually an iterator of 'A' records, so you'll need to iterate through those:

answers = resolver.query(domain, 'A')
for answer in answers:
    print (answer.to_text())
于 2018-03-27T09:44:26.353 回答
3
import dns.resolver

def resolveDNS():
    domain = "google.com" 
    resolver = dns.resolver.Resolver(); 
    answer = resolver.query(domain , "A")
    return answer

resultDNS = resolveDNS()
answer = ''

for item in resultDNS:
    resultant_str = ','.join([str(item), answer])

print resultant_str

所以现在resultant_str是一个字符串类型的变量,它保存用逗号分隔的 A 记录。

于 2018-03-27T10:45:49.707 回答