简单的多线程端口扫描工具
可以接收IP地址作为参数,如果不接参数,则默认扫描本机的端口
1 #!/usr/bin/python 2 3 import socket 4 import time 5 import argparse 6 import threading 7 8 def arg_process(): 9 argparser=argparse.ArgumentParser(description="Port Scaner") 10 argparser.add_argument('-i','--ip',action='store',dest='ip',default='localhost') 11 args=argparser.parse_args() 12 ip=args.ip 13 return ip 14 15 def scan(ip,port): 16 if port>=65535: 17 pass 18 else: 19 addr=(ip,port) 20 sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 21 try: 22 status=sock.connect_ex(addr) 23 if status==0: 24 print "%d opened"%port 25 26 except: 27 pass 28 finally: 29 sock.close() 30 31 def main(start,stop): 32 ip=arg_process() 33 for port in range(start,stop): 34 scan(ip,port) 35 36 37 if __name__=='__main__': 38 t1=time.time() 39 threads=[] 40 for i in range(20): 41 t=threading.Thread(target=main,args=(3277*i,3277*(i+1))) 42 threads.append(t) 43 for t in threads: 44 t.setDaemon(True) 45 t.start() 46 t.join() 47 t2=time.time() 48 delta=t2-t1 49 print "It spend %d seconds"%delta