returning 'A' DNS record in dnspython
Asked Answered
W

2

6

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?

Whereupon answered 27/3, 2018 at 9:40 Comment(1)
have u tried for item in resultDNS: print item? I think that would give u a fair idea whats inside the returned object and how its elements can be accessed.Barrelhouse
B
4
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.

Barrelhouse answered 27/3, 2018 at 10:45 Comment(0)
C
8

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())
Cauthen answered 27/3, 2018 at 9:44 Comment(0)
B
4
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.

Barrelhouse answered 27/3, 2018 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.