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?
Dnspython provides both high and low-level access to the DNS. The high-level classes perform queries for data of a given name, type, and class, and return an answer set. The low-level classes allow direct manipulation of DNS zones, messages, names, and records. Almost all RR types are supported.
An Rdata is typed data in one of the known DNS datatypes, for example type A , the IPv4 address of a host, or type MX , how to route mail. Unlike the DNS RFC concept of RR, an Rdata is not bound to an owner name. Rdata is immutable.
Domain Name System also known as DNS is a phonebook of the internet, which has related to the domain name. DNS translates the domain names to the respective IP address so that browsers can access the resources. Python provides DNS module which is used to handle this translation of domain names to IP addresses.
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())
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With