dnspython模組報錯 AttributeError: 'CNAME' object has no attribute 'address'
阿新 • • 發佈:2020-08-23
有時候用到這個模組的時候會報錯
AttributeError: 'CNAME' object has no attribute 'address'
如下所示
[root@ansible ch01]# ./dnspython_ex1.py Please input a domain: www.baidu.com Traceback (most recent call last): File "./dnspython_ex1.py", line 9, in <module> print(j.address) AttributeError: 'CNAME' object has no attribute 'address'
程式碼是這樣的:
#!/usr/bin/env python
import dns.resolver
domain = raw_input('Please input a domain: ')
A = dns.resolver.query(domain, 'A')
for i in A.response.answer:
for j in i.items:
print j.address
我們只需在最後需要輸出address時在前面增加if判斷
if j.rdtype == 1:
將程式碼修改如下:
#!/usr/bin/env python import dns.resolver domain = raw_input('Please input a domain: ') A = dns.resolver.query(domain, 'A') for i in A.response.answer: for j in i.items: if j.rdtype == 1: print j.address
執行就不會報錯了
[root@ansible ch01]# ./dnspython_ex1.py
Please input a domain: www.baidu.com
14.215.177.38
14.215.177.39