Python uses DNS round robin to detect if the Web server is abnormal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.