最近看到網上有一個例子,是用於通過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=="<!DOCTYPE html>": #一般已經定義好 print ip+" [OK]" else: print ip+" [ERROR]"
最後運行輸出結果如下:
主機ip [OK]