python3 dns轮询业务检测
发布时间:2020-12-20 12:49:11 所属栏目:Python 来源:网络整理
导读:python3中必须使用dnspython3和httplib2!! 直接安装dnspython3和httplib2 ? 本脚本源自 刘天斯的《python 自动化运维技术与最佳实践》 该脚本功能为判断业务是否正常。过程为:首先脚本会向dns服务器提出解析请求,拿到请求返回结果后(一个域名返回多个A
python3中必须使用dnspython3和httplib2!! 直接安装dnspython3和httplib2 ? 本脚本源自 刘天斯的《python 自动化运维技术与最佳实践》 该脚本功能为判断业务是否正常。过程为:首先脚本会向dns服务器提出解析请求,拿到请求返回结果后(一个域名返回多个A记录) 通过模仿浏览器访问业务ip,比对返回的html头是否与预期头相同,进而判断业务是否正常。 在文中脚本使用python2 缩写,在python3 下几乎不可运行。稍加改动脚本如下: ? from dns import resolver#dnspython导入方法 import os import httplib2 iplist=[]#定义域名ip列表变量 appdomain = ‘www.baidu.com‘#定义业务域名 def get_iplist(domain=""): ##解析域名为函数,解析成功将追加到iplist try: A = resolver.query(domain,‘A‘)#解析A记录类型 except Exception as e: print(‘dns resolver error:‘ + str(e)) return for i in A: iplist.append(i)#追加ip到iplist return True def checkip(ip): checkurl = str(ip) + ":80" getcontent="" httplib2.socket.setdefaulttimeout(5)#定义http连接时间超时为5秒 conn = httplib2.HTTPConnectionWithTimeout(checkurl)#创建http连接对象 try: conn.request("GET","/",headers = {"HOST": appdomain}) #发起url请求,添加主机头 ##通过构造html头访问目标业务主机 response = conn.getresponse() getcontent = response.read(15)#获取url前15个字符,做校验用 finally: if getcontent == b"<!DOCTYPE html>" : ##判断返回字符串是否与预期相同 ?测试: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |