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

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

最新文章

  1. 开始用Word 2013来写博客
  2. Endnote专题之--output style相关问题
  3. [Java]Hessian客户端和服务端代码例子
  4. SQL 笔记
  5. Linux chkconfig命令
  6. Eclipse的java代码出错:The import org.apache cannot be resolved
  7. LinkCode 第k个排列
  8. THINKPHP 3.2 PHP SFTP上传下载 代码实现方法
  9. Django 学习笔记(三)模板导入
  10. C语言学生信息管理系统项目源码
  11. 安卓高级6 CoordinatorLayout
  12. 《AutoCAD Civil 3D .NET二次开发》勘误2
  13. java中变量的存储位置
  14. 转:python request属性及方法说明
  15. SpringBoot下如何配置实现跨域请求?
  16. C语言复习:指针知识
  17. mysql 记录的增删改查
  18. android--Git上克隆项目遇到的坑
  19. junit所需要的jar包
  20. 20155331 2016-2017-2 《Java程序设计》第10周学习总结

热门文章

  1. spring的注解AOP配置
  2. 全网最详细的AbstractQueuedSynchronizer(AQS)源码剖析(二)资源的获取和释放
  3. Docker从入门到精通(三)——概念与执行流程
  4. Jenkins pipeline声明式语法
  5. [BUUCTF]REVERSE——[ACTF新生赛2020]easyre
  6. [BUUCTF]REVERSE——rsa
  7. zabbix保姆级安装教程
  8. MAVEN基础讲解
  9. 系统分析师教程(张友生)高清pdf下载
  10. libevent源码学习(1):日志及错误处理