import socket, struct, threading, time, bsddb, os, signal
from sys import argv
global count, db_iptable, allow_store_ip
count=0
thread_list=[]
quit=False
def sigHandler(sig, id):
global quit
if sig == signal.SIGINT:
quit = True
def iptoint(ipstr):
return socket.ntohl(struct.unpack('L',socket.inet_aton(ipstr))[0])
def socksend(host):
global count
connection=socket.socket()
connection.settimeout(1)
if connection.connect_ex((host,80)) == 0:
connection.send('GET /q.php?q=Tiananmen+Square+Protests HTTP/1.0\r\nHost:www.facebook.com/videos/\r\naccept-language:en-us,en;q=0.5\r\naccept-encoding:gzip,deflate\r\nconnection:close\r\naccept:text/xml\r\naccept-charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nuser-agent:Mozilla/5.0\r\n')
if allow_store_ip:
db_ipnew.put(None,host)
else:
connection.close()
if argv[1:] and argv[1] in ('-h', '--help'):
print argv[0], "[from_ip_addr to_ip_addr] \n" ,argv[0], " 66.249.80.1 66.251.80.1"
exit(0)
else:
if len(argv) <3:
print "statement Error!, try ",argv[0],' -h to get more information!'
exit(1)
try:
ip_from=iptoint(argv[1])
ip_to=iptoint(argv[2])
except:
print "statement Error!, try ",argv[0],' -h to get more information!'
exit(1)
if ip_from >ip_to:
print "the front ip address may larger than the end ip address!"
exit(1)
ip_addr=ip_from
db_ipnew=bsddb.db.DB()
db_ipold=bsddb.db.DB()
allow_store_ip=False
signal.signal(signal.SIGINT, sigHandler) #Handle Ctrl+c
try:
db_ipnew.open(os.path.normpath(os.path.expanduser('~/.IPTable_GFW.db')),"new",bsddb.db.DB_QUEUE,bsddb.db.DB_CREATE|bsddb.db.DB_THREAD)
db_ipold.open(os.path.normpath(os.path.expanduser('~/.IPTable_GFW.db')),"old",bsddb.db.DB_BTREE,bsddb.db.DB_CREATE|bsddb.db.DB_THREAD)
newips=db_ipnew.values()
for i in newips:
db_ipold.put(i,None)
db_ipnew.close()
db_ipnew=bsddb.db.DB()
db_ipnew.open(os.path.normpath(os.path.expanduser('~/.IPTable_GFW.db')),"new",bsddb.db.DB_QUEUE,bsddb.db.DB_CREATE|bsddb.db.DB_THREAD|bsddb.db.DB_TRUNCATE)
Table_OK=True
except Exception:
Table_OK=False
if Table_OK and db_iptable.stat()['nkeys']>1:
ips=db_ipold.keys()
ip_index=0
while ip_index< len(ips):
if count<100 and not quit:
ip_str=ips[ip_index]
my_thread=threading.Thread(target=socksend, args=(ip_str,))
thread_list.append(my_thread)
my_thread.start()
ip_index+=1
count += 1
else:
for i in thread_list:
i.join()
thread_list=[]
count=0
allow_store_ip=Table_OK
print 'allow store ip',allow_store_ip
while True:
if count <100:
ip_addr+=1
ip_str=socket.inet_ntoa(struct.pack('L',socket.ntohl(ip_addr)))
my_thread=threading.Thread(target=socksend, args=(ip_str,))
thread_list.append(my_thread)
my_thread.start()
count +=1
else:
for i in thread_list:
i.join()
thread_list=[]
count=0
# time.sleep(1)
if ip_addr > ip_to or quit:
for i in thread_list:
i.join()
exit(0)
将上述保存为一py文件,执行
python.exe 文件名 起始ip 终止IP即可。该IP段须位于境外。