最近看到网上有一个例子,是用于通过DNS轮询来查看同个DNS下,多个ip对应服务页面监控的脚本,自己在试验过程中,发现访问有些网站会出现conn.getresponse()为空的情况,通过conn.getresponse().status、conn.getresponse().reason得出是303 SEE OTHER,这种情况不能做出服务是否正常的判断,所以通过wireshark抓包来查看返回200 OK上面一个包,然后通过分析包,加到headers进行访问。具体过程如下:
1、打开wireshark,启动网卡,在浏览器进行网页访问,过滤掉没用的包http and ip.addr==服务端地址 and tcp.port == 80
2、选中右击相应的包,选择Follow TCP stream,得到类似如下的过程....................GET / HTTP/1.1
Host: 主机DNS
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Cookie: 对应的cookie
HTTP/1.1 200 OK
Date: Fri, 25 Mar 2016 03:27:55 GMT
以下省略......
3、用以下代码进行访问测试def checkip(ip):
url=ip+":80" #端口ip依据实际而定
getcontent=""
httplib.socket.setdefaulttimeout(5)
conn = httplib.HTTPConnection(url)
try:
conn.request("GET","/",headers={"Host":"主机DNS","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/w
ebp,*/*;q=0.8","Connection":"keep-alive","Cookie":"对应的cookie"})
r = conn.getresponse()
getcontent = r.read(15)
print getcontent
finally:
if getcontent=="html>": #一般已经定义好
print ip+" [OK]"
else:
print ip+" [ERROR]"
最后运行输出结果如下:主机ip [OK]