继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

python检测主机状态

linux零基础学习视频
关注TA
已关注
手记 276
粉丝 29
获赞 122

#!/usr/bin/python

import dns.resolver, os, httplib

iplist = []
appdomain = "cdsendmail.ecwise.com"

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 == "<!DOCTYPE html ":
                    print ip + " [OK]"
            else:
                    print ip + " [error]"

def getip(domain):

try:
a = dns.resolver.query(domain, 'A')
except Exception as E:
print "dns resolve error"+str(E)
return 0
for i in a.response.answer:
for j in i.items:
iplist.append(j.address)
print iplist
return True

if name == "main":
if getip(appdomain) and len(iplist) > 0:
for ip in iplist:
checkip(ip)
else:
print "dns resolve error!"

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP