python的DNS

模块简介

dnspython` 是 Python 实现的一个 DNS 工具包,支持几乎所有的记录类型,可以用于查询、传输并动态更新 ZONE 信息,同时支持 TSIG 验证消息和 EDNSO。在系统管理方面,我们可以利用其查询功能来实现 DNS 服务器监控以及解析结果的校验,可以代替 nslookup 及 dig 等工具,轻松做到与现有平台的整合

DNS 记录

  • A 记录,将主机名转换成 IP 地址
  • MX 记录,邮件交换记录,定义邮件服务器的域名
  • CNAME 记录,指别名记录,实现域名之间的映射
  • NS 记录,记录区域的域名服务器及授权子域
  • PTR 记录,反向解析,与 A 记录相反,将 IP 转换成主机名
  • SOA 记录,SOA 标记,一个起始授权区的定义

python 的 DNS

DNS 常见解析类型示例说明

  • 常见的 DNS 解析类型包括A,MX,NS,CNAME,利用 dnspython 的 dns.resolver
  • query 方法可以简单实现这些 DNS 类型的查询,为后面要实现的功能提供数据来源,比如对一个使用 DNS 轮询业务的域名进行可用性监控,需要得到当前的解析结果

DNS 常用功能实战操作

  • 演示 1 : 常用 DNS 功能
  1. 使用pip install dnspythonpip installmysql.connector 安装所需模块
  2. 导入模块:
import dns.resolver
  1. 尝试解析一条记录:
A = dns.resolver.query("www.buaihechengzizhi.com","A") # 表示解析A记录
print(A.response.answer)

运行结果:

[<DNS www.buaihechengzizhi.com. IN A RRset>]

可以看出,程序给出了应答,返回了一个对象,我们来分析这个对象:

从结果上看,它是一个 list,我们采用循环的方式打印:

for item in A.response.answer:
    print(item)

运行结果:

www.buaihechengzizhi.com. 0 IN A 47.104.14.19

DNS 服务器给出的应答是,"www.buaihechengzizhi.com" 域名所对应的 IP 地址是 47.104.14.19

我们尝试解析一下其他类型的记录:

cname = dns.resolver.query("xunanmu.buaihechengzizhi.com","CNAME")
for item in cname.response.answer:
    print(item)

运行结果:

xunanmu.buaihechengzizhi.com. 600 IN CNAME www.xunanmu.com.

这里的 "xunanmu.buaihechengzizhi.com" 便是 "www.xunanmu.com" 的别名

大家可自行尝试一些其他类型的记录,关于DNS服务器的搭建,将在以后详细说明

  • 演示 2 : 网站监控指标

现在我们做一个监控网站性能指标的程序:

import dns.resolver as dns # 取别名为dns
import http.client as httplib

注:这里我用的 python 版本是 python3.7,如果是 python2.x,可直接导入 httplib,不需要安装,这是 python 的核心模块

定义我们要监控的网站:

url = "www.buaihechengzizhi.com"
A = dns.query(url,"A")

然后我们用 http 模块中的 HttpConnection 进行连接

for i in A.response.answer:
    ipaddress = i.items[0].address
    conn = httplib.HTTPConnection(ipaddress)
    conn.request("GET","/") # 发起get请求,访问根路由
    r = conn.getresponse() # 获取结果
    print(r)

我们先运行看一下结果:

<http.client.HTTPResponse object at 0x000001B19ACD2C50>

没有报错表示已经可以访问网站了,但是它返回的是一个对象,我们看不懂,只好手工解析一下它:

print(r.read())

运行结果:

b'<!DOCTYPE html>\n<html>\n\n<head>\n    <!-- Meta, title, CSS, favicons, etc. -->\n    <meta charset="utf-8">\n    <meta http-equiv="X-UA-Compatible" content="IE=edge">\n    
<meta name="viewport" content="width=device-width, initial-scale=1">\n    <meta name="description" content="Dmego Home">\n    <meta name="keywords" content="Dmego,Homepage">\n    <meta name="author" content="Dmego">\n    <title>Orange Home</title>\n    <link rel=\'stylesheet\' type=\'text/css\' href=\'https://cdn.webfont.youziku.com/webfonts/nomal/123918/33195/5d21f88af629d8080c5960eb.css\' />\n    <link rel="stylesheet" type="text/css" href="./assets/css/vno.css">\n    <link rel="stylesheet" type="text/css" href="./assets/css/iconfont.css">\n    <link rel="apple-touch-icon" href="./apple-touch-icon.png">\n    <link rel="icon" href="./favicon.ico">\n</head>\n\n<body>\n    <span class="mobile btn-mobile-menu">\n        <i class="social iconfont icon-list btn-mobile-menu__icon"></i>\n        <i class="social iconfont icon-angleup btn-mobile-close__icon hidden"></i>\n    </span>\n    <header id="panel" class="panel-cover">\n        <div class="panel-main">\n            <div class="panel-main__inner panel-inverted">\n                <div 
class="panel-main__content">\n                    <div class="ih-item circle effect right_to_left">            \n                        <a href="#" title="" class="blog-button">\n                            <div class="img"><img src="./img/logo2.jpg" alt="img" class="js-avatar iUp profilepic"></div>\n                            <div class="info iUp">\n                                <div class="info-back">\n                                    <h2> \n                                        \n
                Fighting\n                                        \n                                    </h2>\n                                    <p>\n
                 \n                                            2020 \xc2\xb7 \xe5\x8a\xaa\xe5\x8a\x9b\n                                        \n
      </p>\n                                </div>\n                            </div>\n                        </a>\n                    </div>\n\n                    <!-- <a href="#" title="Dmego Home" class="profilepic">\n                        <img src="./img/logo2.jpg" width="80" alt="Dmego`s logo" class="js-avatar iUp show" />\n
       </a> -->\n                    <h1 class="panel-cover__title panel-title iUp">\n                        <a href="#" title="Dmego Home">\xe4\xb8\x8d\xe7\x88\xb1\xe5\x96\x9d\xe6\xa9\x99\xe5\xad\x90\xe6\xb1\x81</a>\n                    </h1>\n                    <p class="panel-cover__subtitle panel-subtitle iUp">\xe6\xb5\x81\xe5\x8a\xa8\xe7\x9a\x84\xe9\x9f\xb3\xe7\xac\xa6\xe6\x8b\xa8\xe5\x8a\xa8\xe5\xbf\x83\xe5\xbc\xa6</p>\n                    <hr class="panel-cover__divider iUp" />\n                    <p id="description" class="panel-cover__description iUp">\n                        <br/>\n                        <strong></strong>\n                    </p>\n                    <div class="navigation-wrapper iUp">\n                        <div>\n                            <nav class="cover-navigation cover-navigation--primary">\n
    <ul class="navigation">\n                                    <li class="navigation__item">\n                                        <a href="/" class="blog-button">\xe9\xa6\x96\xe9\xa1\xb5</a>\n                                    </li>\n                                    <li class="navigation__item">\n                                        <a href="./blog" class="blog-button" target="_blank">\xe5\x8d\x9a\xe5\xae\xa2</a>\n                                    </li>\n                                    <li class="navigation__item">\n                                        <a href="#" class="blog-button" target="_blank">\xe7\xae\x80\xe5\x8e\x86</a>\n                                    </li>\n                                    <li class="navigation__item">\n                                        <a href="#" class="blog-button" target="_blank">\xe5\x85\xb3\xe4\xba\x8e</a>\n                                    </li>\n                                </ul>\n                            </nav>\n                        </div>\n
           <div class="iUp">\n                            <nav class="cover-navigation navigation--social">\n                                <ul class="navigation">\n
                           <li class="navigation__item">\n                                        <a href="#" title="github" target="_blank">\n
            <i class=\'social iconfont icon-github\'></i>\n                                            <span class="label">github</span>\n
   </a>\n                                    </li>\n                                    <li class="navigation__item">\n                                        <a href="#" title="cnblogs" target="_blank">\n                                            <i class=\'social iconfont icon-cnblogs\'></i>\n                                            <span class="label">cnblogs</span>\n                                        </a>\n                                    </li>\n\n                                    <li class="navigation__item">\n                                        <a href="#" title="zhihu" target="_blank">\n                                            <i class=\'social iconfont icon-zhihu\'></i>\n                                            <span class="label">zhihu</span>\n                                        </a>\n                                    </li>\n                                    <li class="navigation__item">\n                                        <a href="#" title="email">\n
     <i class=\'social iconfont icon-email\'></i>\n                                            <span class="label">email</span>\n                                        </a>\n                                    </li>\n                                </ul>\n                            </nav>\n                        </div>\n                    </div>\n                </div>\n            </div>\n            <div class="panel-cover--overlay cover-slate"></div>\n        </div>\n        <!-- <div class="beian iUp">\n        
    <a class="icp" href="http://www.miitbeian.gov.cn/publish/query/indexFirst.action" target="_blank">XXX</a>\n            <a class="gwab" href="http://www.beian.gov.cn/portal/recordQuery" target="_blank">XXX</a>\n        </div> -->\n        <!--<div class="remark iUp">-->\n        <!--    <p class="power">Powered By -->\n        <!--        <a href="#" target="_blank"> JsonBird </a> And -->\n        <!--        <a href="# target="_blank"> Hitokoto </a>-->\n        <!--    </p>-->\n        <!--</div>-->\n    </header>\n    <script type="text/javascript" src="./assets/js/jquery.min.js"></script>\n    <script type="text/javascript" src="./assets/js/fetch.min.js"></script>\n    <script type="text/javascript" src="./assets/js/main.js"></script>\n</body>\n\n</html>'

虽然结构仍然很混乱(因为这是一个byte列表),但至少我们仍然能看出来这是 HTML 页面,是服务器给我们的响应,到这里我们已经结束了;

仅发起请求的代码:

import http.client as httplib
conn = httplib.HTTPConnection("www.buaihechengzizhi.com")
conn.request("GET","/")
r = conn.getresponse()
print(r.read().decode("utf-8"))

搭建本地服务器:

在命令行中运行python -m http.server 8000便可在127.0.0.1:8000中运行一个 http 服务器

    rgvkfvemgs
    rgvkfvemgs  2020-11-17, 17:25

    python的DNS - 橙香静屋
    rgvkfvemgs http://www.g8ano7t7azv719mt0ry7441p69e7r93xs.org/
    [url=http://www.g8ano7t7azv719mt0ry7441p69e7r93xs.org/]urgvkfvemgs[/url]
    argvkfvemgs