python dns.resolver get DNS record type

Jim picture Jim · May 5, 2011 · Viewed 19.1k times · Source

I am learning how to use the python dns object. Quick question as I see many examples show methods of using the dns.resolver method with the DNS record type(CNAME, NS, etc). Is there a way to use this dns object to query a DNS name and pull it's resolution with the record type. Similar to what DIG supplies in the answer section.

Thanks,

Jim

Answer

Mike Barkas picture Mike Barkas · Jul 12, 2017

You can get the type with rdatatype

>>> import dns.resolver
>>> answer = dns.resolver.query('google.com')
>>> rdt = dns.rdatatype.to_text(answer.rdtype)
>>> print(rdt)
A