One day I am writing a script digging DNS records. A weird error appears during testing:
<class 'dns.rdtypes.ANY.CNAME.CNAME'> Traceback (most recent call last): File "C:\Users\xyz\test.py", line 65, in <module> if isinstance(rr, dns.rdtypes.IN.A.A): AttributeError: 'module' object has no attribute 'A'
The related code is:
import dns.resolver ... ... if rrset: for rr in rrset: if isinstance(rr, dns.rdtypes.IN.A.A): print 'DNS A record:' + domain_name + ':' + rr.address elif isinstance(rr, dns.rdtypes.ANY.CNAME.CNAME): print 'DNS CNAME record:' + domain_name + ':' + rr.target.to_text()
It turns out that, if an instance of class “dns.rdtypes.IN.A.A” is never generated before this “if isinstnace()” line. Python won’t know the class “dns.rdtypes.IN.A.A”. Thus it will raise the error.
So the solution is to explicitly import dns.rdtypes.IN.A, or manually create an A record before the isinstance() check.