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

python使用dns轮循检测web服务器是否异常

时间:2018-02-12 20:04:05      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:浏览器   request   ima   没有   final   end   lib   字符串   默认   

我使用的是python2.7,我本来另装了一个python3.6,发现无法安装dnspython,于是只能换回来了
import dns.resolver #这个需要另外下载并安装(下载地址www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz 解压之后,python setup.py install)
import os
import httplib #因为要用到http?
iplist=[] #存储查到的ip
appdomain="www.baidu.com" #查询的网站服务器
def get_iplist(domain=""): #这应该是说如果domain没有值,则默认为空
try: #捕获异常
A=dns.resolver.query(domain,‘A‘)
print (‘hi‘)
except Exception,e:
print ‘dns wrong:‘+str(e)
return
for i in A.response.answer:
for j in i.items:
print ("ip is %s " % j)
newj=str(j) #如果不转换为字符串格式,会报错
#print type(newj)
iplist.append(newj) #把查询到的ip放到列表中
return True
def checkip(ip): #模拟浏览器访问,查询服务器是否异常
checkurl=ip+‘:80‘
getcontent=""
httplib.socket.setdefaulttimeout(5) #设置默认超时时间
conn=httplib.HTTPConnection(checkurl)
try:
conn.request("GET","/",headers={"HOST":appdomain}) #获取头文件内容
r=conn.getresponse()
getcontent=r.read(15) #取头文件的一部分进行对比即可
print getcontent
finally:
if getcontent=="": #注意,这里区分大小写,因为是字符串比较
print ip+" [ok]"
else:
print ip+" [error]"
if name=="main":
#if get_iplist(appdomain) and len(iplist)>0:
if get_iplist(appdomain):
for ip in iplist: #逐个检查
checkip(ip)
else:
print "dns resolver error."

技术分享图片

python使用dns轮循检测web服务器是否异常

标签:浏览器   request   ima   没有   final   end   lib   字符串   默认   

原文地址:https://www.cnblogs.com/biaopei/p/8445154.html

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