1 #!/usr/bin/env python 2 # -*- coding: gbk -*- 3 # -*- coding: utf_8 -*- 4 # Date: 2015年9月11日 5 # Author:蔚藍行 6 # 博客 http://m.survivalescaperooms.com/duanv/ 7 8 import requests 9 import threading10 import Levenshtein11 import re12 13 def scan(original_r,cip,ip_begin,original_match,header):14 ip=cip+str(ip_begin)15 try:16 r=requests.get('http://'+ip,headers=header,timeout=1)17 except Exception:18 pass19 else:20 if(r.status_code==original_r.status_code):21 if r.content==original_r.content:22 PRint '---everything is match!---/n'+ip+'/n--------------------------/n/n/n',23 else:24 if Levenshtein.ratio(r.text,original_r.text)>0.8: 25 match=re.search(r"<title>(.*?)</title>",r.content)26 try:27 if match==original_match or match.group()==original_match.group():28 print '--matches>0.8-same title--/n'+ip+'/n--------------------------/n/n/n',29 else:30 print '--matches>0.8-diff title--/n'+ip+'/n--------------------------/n/n/n',31 except Exception:32 if match==None:33 #掃描網頁無標題34 print '-matches>0.8-none title-s-/n'+ip+'/n--------------------------/n/n/n',35 else:36 #原始網頁無標題37 print '-matches>0.8-none title-o-/n'+ip+'/n--------------------------/n/n/n',38 39 def loop(original_r,cip,original_match,header):40 global ip_begin,ip_end,mutex41 while 1:42 mutex.acquire()43 if ip_begin > ip_end:44 mutex.release()45 break46 ip=ip_begin47 ip_begin += 148 mutex.release()49 scan(original_r,cip,ip,original_match,header)50 51 def start():52 global ip_begin,ip_end,mutex53 54 ip_begin=155 ip_end=25456 mutex=threading.Lock()57 58 cip='180.97.33.'59 address='www.baidu.com'60 61 #cip='220.181.136.'62 #address='www.219.me'63 64 header={"host":address,"Accept-Encoding":"identity","User-Agent":""}65 r=requests.get('http://'+address,headers=header)66 67 original_match=re.search(r"<title>(.*?)</title>",r.content)68 69 threads=[]70 for i in range(254):71 threads.append(threading.Thread(target=loop,args=(r,cip,original_match,header)))72 for t in threads:73 t.start()74 75 if __name__ == '__main__':76 start()
1,由于是多線程,輸出時如果用print xxx會出現因線程搶占而造成的輸出亂序,改用print xxx+‘/n’,可以不用線程鎖并解決這個問題
2,掃描網段時請求頭加上host:domain_name,可以解決單一IP對應多域名的問題
3,發送請求時將UA頭置空,可以防止某些網站服務器返回內容過大和不完全相同的問題,提高效率。比如請求百度,有UA頭情況下返回的頁面內容相當大,而且不同IP返回內容有細微差別,腳本就會進行相似度比較,耗時巨大,如果置空UA頭,請求響應內容就比較小,而且不同IP返回內容相同
新聞熱點
疑難解答