returning 'A' DNS record in dnspython

匆匆过客 提交于 2019-12-08 08:52:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!