Python的dnspython库使用指南
因为平时在测试DNS的时候有些操作手动完成不方便,所以需要用到脚本,而在Python里dnspython这个用于DNS操作的库十分强大,但是无奈网上大部分资料只列举了少部分的用法,所以记录一下我平时使用到的功能,基本上已经能应付大部分的使用场景了。想具体了解dnspython可以登录官方网站阅读使用文档.
常用工具
最常用的用法是调用默认的resolver发送解析请求,如
from dns import resolver
ans = resolver.query("www.baidu.com", "A")
print("qname:",ans.qname)
print ("reclass:",ans.rdclass)
print ("rdtype:",ans.rdtype)
print ("rrset:",ans.rrset)
print ("response:",ans.response)
结果为
('qname:', <DNS name www.baidu.com.>)
('reclass:', 1)
('rdtype:', 1)
('rrset:', <DNS www.a.shifen.com. IN A RRset>)
('response:', <DNS message, ID 64940>)
在这里解析任务默认发送给系统默认的dns服务器,其中比较重要的是response,在dnspython的官方文档里,response属于类dns.message.Message,这个类也是许多DNS query请求的返回结果,下面详细介绍下这个类。
类的主要成员变量有:
int flags #The DNS flags of the message.
int id #The query id; the default is a randomly chosen id.
list of RRset addictional
list of RRset answer
list of RRset authority
flags属于返回DNS报文的标志位(详见《TCP/IP详解(卷一)》关于DNS的部分),可以利用以下代码打印DNS报文的各个标志位:
#!/bin/env python2.7
ans = resolver.query("www.baidu.com", "A") def FlagCount(flags, pos):
if (flags/(2**pos))%2 == 1:
return True
else:
return False def GetFlags(flags):
QR_pos = 15
AA_pos = 10
TC_pos = 9
RD_pos = 8
RA_pos = 7
QR_flag = FlagCount(flags, QR_pos)
AA_flag = FlagCount(flags, AA_pos)
TC_flag = FlagCount(flags, TC_pos)
RD_flag = FlagCount(flags, RD_pos)
RA_flag = FlagCount(flags, RA_pos)
flag_dic = {"QR":QR_flag, "AA":AA_flag, "TC":TC_flag, "RD":RD_flag, "RA":RA_flag}
print "flag:",
for flag in flag_dic:
if flag_dic[flag]:
print flag, flags = ans.response.flags GetFlags(flags)
返回结果为:
flag: AA RD QR RA
另外一个比较重要的类就是RRset,通常返回的三个section信息都使用这个类封装,常用的用法是使用类函数to_text()令解析结果以字符串形式显示。如:
ans = resolver.query("www.baidu.com", "A")
for i in ans.response.answer:
print i.to_text()
结果为:
www.baidu.com. 1200 IN CNAME www.a.shifen.com.
www.a.shifen.com. 119 IN A 220.181.112.244
www.a.shifen.com. 119 IN A 220.181.111.188
使用实例:
A记录查询
#!/usr/bin/env python
import dns.resolver
domain = raw_input('Please input an domain: ')
A = dns.resolver.query(domain, 'A')
for i in A.response.answer:
for j in i.items:
print j.address
MX记录查询(注意输入域名不包括www)
#!/usr/bin/env python
import dns.resolver
domain = raw_input('Please input an domain: ')
MX = dns.resolver.query(domain, 'MX')
for i in MX:
print 'MX preference =', i.preference, 'mail exchanger =', i.exchange
NS记录查询
#!/usr/bin/env python
import dns.resolver
domain = raw_input('Please input an domain: ')
ns = dns.resolver.query(domain, 'NS')
for i in ns.response.answer:
for j in i.items:
print j.to_text()
CNAME记录查询
#!/usr/bin/env python
import dns.resolver
domain = raw_input('Please input an domain: ')
cname = dns.resolver.query(domain, 'CNAME')
for i in cname.response.answer:
for j in i.items:
print j.to_text()
Python的dnspython库使用指南的更多相关文章
- python的PEP8 代码风格指南
PEP8 代码风格指南 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 知识点 代码排版 字符串引号 表达式和语句中的空格 注释 版本 ...
- python 协程库gevent学习--gevent数据结构及实战(三)
gevent学习系列第三章,前面两章分析了大量常用几个函数的源码以及实现原理.这一章重点偏向实战了,按照官方给出的gevent学习指南,我将依次分析官方给出的7个数据结构.以及给出几个相应使用他们的例 ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
- Python的常用库
读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...
- Python底层socket库
Python底层socket库将Unix关于网络通信的系统调用对象化处理,是底层函数的高级封装,socket()函数返回一个套接字,它的方法实现了各种套接字系统调用.read与write与Python ...
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
- python使用cookielib库示例分享
Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持,下面是使用示例 该模块主要功能是提供可存储cookie的对象.使用此模块捕获 ...
随机推荐
- WPF 窗口大小自适应
在设置桌面不同分辨率以及较大DPI下,窗口如何显示的问题. 方案一 设置窗口最大值和最小值显示 通过对比当前屏幕的可显示区域,将窗口高宽最大值和最小值,设置为窗口的实际高宽(此例中仅设置高度) 界面设 ...
- 初识 MongoDB,MongoDB 的安装运行
1. MongoDB 非关系型数据库 MongoDB是一个基于分布式文件存储的数据库,由C++语言编写.目的是为WEB应用提供扩展的高性能的数据存储解决方案.MongoDB是一个介于关系型数据库和 ...
- python之字符串反转
def main(): a = "abcdefg" a = a[::-1] print(a) if __name__ == '__main__': main()
- SpringBoot 之Actuator.
一.Actuator 介绍 Actuator 是 SpringBoot 项目中一个非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管.审计.收集应用的运行情况. ...
- 熟悉常用的HBase操作,编写MapReduce作业
1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age) 201 ...
- 在ubuntu16.04中再次体验.net core 2.0
在上一篇文章中在ubuntu16.04中初次体验.net core 2.0 简单介绍了一下ubuntu中运行.net core 2.0.配置nginx反向代理以及安装supervisor守护进程……本 ...
- 【代码笔记】Web-CSS-CSS Border(边框)
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 【阿里云】在 Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务
Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务 一.安装 Filezilla Server 下载最新版本的 Filezilla Server ...
- vmware 开启VM的硬件cpu虚拟化功能
物理机的cpu硬件虚拟化功能,通过开启bios中的设置,而vmware中创建的虚拟机也可以开启该特性,如下. 这样的话,就直接可以基于这些VM安装openstack计算节点.控制节点.网络节点了.
- postman测试方法,出现400错误码
下午毛概课上帮同学debug了个错误: postman测试 ,得到返回 400的状态码错误: 查询博客: https://blog.csdn.net/zhangmengleiblog/article/ ...