码迷,mamicode.com
首页 > 编程语言 > 详细

DNS处理模块dnspython

时间:2017-01-29 21:32:52      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:read   getc   add   oct   记录   error:   please   content   finally   

http://www.dnspython.org/kits/1.9.4/

python setup.py install

A记录查询

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记录

#!/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

 

Please input an domain: zte.com
MX preference = 0 mail exchanger = mx.zte.com.

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()

 

 

 

#!/usr/bin/python

import dns.resolver
import os
import httplib

iplist=[]    #定义域名IP列表变量
appdomain="www.google.com.hk"    #定义业务域名

def get_iplist(domain=""):    #域名解析函数,解析成功IP将追加到iplist
    try:
        A = dns.resolver.query(domain, ‘A‘)    #解析A记录类型
    except Exception,e:
        print "dns resolver error:"+str(e)
        return
    for i in A.response.answer:
        for j in i.items:
            iplist.append(j.address)    #追加到iplist
    return True

def checkip(ip):
    checkurl=ip+":80"
    getcontent=""
    httplib.socket.setdefaulttimeout(5)    #定义http连接超时时间(5秒)
    conn=httplib.HTTPConnection(checkurl)    #创建http连接对象

    try:
        conn.request("GET", "/",headers = {"Host": appdomain})  #发起URL请求,添加host主机头
        r=conn.getresponse()
        getcontent =r.read(15)   #获取URL页面前15个字符,以便做可用性校验
    finally:
        if getcontent=="<!doctype html>":  #监控URL页的内容一般是事先定义好,比如“HTTP200”等
            print ip+" [OK]"
        else:
            print ip+" [Error]"    #此处可放告警程序,可以是邮件、短信通知

if __name__=="__main__":
    if get_iplist(appdomain) and len(iplist)>0:    #条件:域名解析正确且至少要返回一个IP
        for ip in iplist:
            checkip(ip)
    else:
        print "dns resolver error."

 

DNS处理模块dnspython

标签:read   getc   add   oct   记录   error:   please   content   finally   

原文地址:http://www.cnblogs.com/stonerainjc/p/6357706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!