Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning 'A' DNS record in dnspython

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?

like image 414
None Avatar asked Mar 27 '18 09:03

None


People also ask

How dnspython works?

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.

What is DNS Rdata?

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.

What is DNS resolver Python?

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.


2 Answers

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())
like image 99
Jimmy Lee Jones Avatar answered Nov 08 '22 19:11

Jimmy Lee Jones


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.

like image 28
Paandittya Avatar answered Nov 08 '22 21:11

Paandittya