问题
I am using dnspython to get the 'A' record and return the result (IP address for a given domain).
I have this simple testing python script:
import dns.resolver
def resolveDNS():
domain = "google.com"
resolver = dns.resolver.Resolver();
answer = resolver.query(domain , "A")
return answer
resultDNS = resolveDNS()
print resultDNS
However, the output is:
<dns.resolver.Answer object at 0x0000000004F56C50>
I need to get the result as a string. If it is an array of strings, how to return it?
回答1:
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
So now the resultant_str
is a variable of type string that holds A records separated by comma.
回答2:
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())
来源:https://stackoverflow.com/questions/49509431/returning-a-dns-record-in-dnspython