TCP connection scanning, based on sockets, uses threading to implement multi-threading and to control the number of processes.
Note: The Threads_limit variable depends on the host, and there are too many definitions to make the following error:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/86/F2/wKiom1fPfPGTqgnjAAAL7L75bGQ904.png-wh_500x0-wm_3 -wmp_4-s_3950277187.png "title=" 1.png "alt=" Wkiom1fpfpgtqgnjaaal7l75bgq904.png-wh_50 "/>
Code:
Import socketimport threading#change these vars.start_port = 20end_port = 1000host = ' 8.8.8.8 ' threads_limit = 600open_port = []mylock = threading. Lock () Def scan_port (port): global mylock mysock = socket.socket (Socket.af_inet, socket. SOCK_STREAM) if (MYSOCK.CONNECT_EX ((host, port)) == 0): mylock.acquire () open_ Port.append (port) mylock.release () for will_scan_port In range (start_port, end_port+1): mythread = threading. Thread (target = scan_port, args = (Will_scan_port,)) mythread.start ( ) #limit thread number while (Len (Threading.enumerate ()) >= 600): pass print open_port
This article is from "retrograde person" blog, declined reprint!
Simple port Scan