returning 'A' DNS record in dnspython

None picture None · Mar 27, 2018 · Viewed 7.2k times · Source

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?

Answer

Jimmy Lee Jones picture Jimmy Lee Jones · Mar 27, 2018

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())