zoukankan      html  css  js  c++  java
  • dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'

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

    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
    
  • 相关阅读:
    [那些你所不知道的鬼畜写法]数据结构小探
    索引
    数据类型
    存储引擎
    事务
    视图、触发器、存储过程、自定义函数
    约束
    数据库介绍
    线程基础、线程池
    操作系统基础
  • 原文地址:https://www.cnblogs.com/chenjo/p/13549193.html
Copyright © 2011-2022 走看看