标签:
看了大神天斯的书,特此做学习笔记,实现方式略微不同是python3.5
大部分的DNS解析都是一个域名对应一个IP地址,但是通过DNS轮询可做到一个域名对应多个IP,从而实现最简单且高效的负载均衡。
1, 实现步骤
1) 实现域名的解析,获取域名所有的A记录解析IP列表
2) 对IP列表进行HTTP级别的探测
2, 代码
#!/usr/local/bin/python3.5 #-*- coding:utf-8 -*- import dns.resolver import httplib2 iplist = [] appdomain = "www.a.com" def get_iplist(domain=""): try: A = dns.resolver.query(domain,"A") except Exception as e: print("dns resolver error:" + str(e)) return for i in A.response.answer: for j in i.items: iplist.append(j.address) return True def checkip(ip): checkurl = ‘http://‘ + ip + ":80" getcontent = "" httplib2.socket.setdefaulttimeout(5) conn = httplib2.Http() try: resp,getcontent=conn.request(checkurl) finally: if resp[‘status‘]== "200": print(ip+"[OK]") else: print(ip+"[Error]") if __name__ == "__main__": if get_iplist(appdomain) and len(iplist) > 0: for ip in iplist: checkip(ip)
结果如图
END!
标签:
原文地址:http://www.cnblogs.com/changbo/p/5588589.html