I use python2.7, I originally installed a python3.6, found unable to install Dnspython, so can only change back
Import Dns.resolver #这个需要另外下载并安装 (www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz after decompression, Python setup.py install)
Import OS
Import Httplib #因为要用到http?
Iplist=[] #存储查到的ip
Appdomain= "Www.baidu.com" #查询的网站服务器
def get_iplist (domain= ""): #这应该是说如果domain没有值, default is empty
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) #如果不转换为字符串格式, will error
#print type (NEWJ)
Iplist.append (NEWJ) #把查询到的ip放到列表中
Return True
def checkip (IP): #模拟浏览器访问, query server is abnormal
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 (#取头文件的一部分进行对比即可)
Print GetContent
Finally
If getcontent== "": #注意, here is case-sensitive, because it is a string comparison
Print ip+ "[OK]"
Else
Print ip+ "[ERROR]"
Ifname=="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 uses DNS round robin to detect if the Web server is abnormal