The example in this paper describes the multi-threaded port scanning function implemented by Python. Share to everyone for your reference, as follows:
The following program gives the Python code for multithreaded scanning of a given IP host
#!/usr/bin/env python#encoding:utf-8import socket, sys, thread, Timeopenportnum = 0socket.setdefaulttimeout (3) def Usage (): print "" Usage:scan the port of one Ip:python port_scan_multithread.py-o <ip> Scan the port of one Ip:python port_scan_multithread.py-m <ip1, IP2, IP3, ip4 ...> ' print ' Exit ' Sys.exit (1) def socket_port (IP, P ORT): Global openportnum If port > 65535:print ' port scanning beyond the port range, interrupt to scan ' Sys.ex It (1) s = socket.socket (socket.af_inet, socket. SOCK_STREAM) result = S.CONNECT_EX ((IP, PORT)) if (result = = 0): Print IP, PORT, ' is open ' openportnum + = 1 s.close () def start_scan (IP): for port in range (0, 65535+1): Thread.start_new_thread (socket_port, (IP, int (port))) Time.sle EP (0.006) If __name__ = = ' __main__ ': t = 0 if Len (sys.argv) <2 or sys.argv[1] = = '-H ': usage () elif sys.argv[1] = = '-o ': one_ip = raw_input (' Please input IP of scanning: ') T = time.time () start_scan (ONE_IP) elIf sys.argv[1] = = '-M ': many_ip = raw_input (' Please input many IP of scanning: ') ip_seg = Many_ip.split (', ') T = Time.time () for I in Ip_seg:start_scan (i) Print print ' Total open port was%s, scan used time is:%f '% (OPENP Ortnum, Time.time ()-T)
Run