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
    
  • 相关阅读:
    Redis主从复制
    POI导出给指定单元格添加背景色
    Mybatis的resultMap使用
    前后端分离
    日常总结
    java环境变量配置
    Java线程池七个参数详解
    java中常见的锁
    Linux定时任务
    SQL语句的整理
  • 原文地址:https://www.cnblogs.com/chenjo/p/13549193.html
Copyright © 2011-2022 走看看