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.
本文介绍了一个在使用Python DNS模块时遇到的AttributeError异常,并详细解释了错误原因及如何通过明确导入类或提前创建实例来避免该错误。

被折叠的 条评论
为什么被折叠?



