有时候用到这个模块的时候会报错

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

相关文章:

  • 2021-08-29
  • 2021-06-16
  • 2021-04-07
  • 2021-11-03
  • 2021-05-24
  • 2021-04-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-07
  • 2021-09-02
  • 2022-12-23
  • 2021-04-19
  • 2021-09-08
  • 2021-12-17
相关资源
相似解决方案