我正在使用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>
我需要将结果作为字符串。如果它是一个字符串数组,如何返回?
答案 0 :(得分:0)
你得到的答案实际上是一个&#39; A&#39;记录,因此您需要遍历这些记录:
answers = resolver.query(domain, 'A')
for answer in answers:
print (answer.to_text())
答案 1 :(得分:0)
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条记录。