"""
Project name: Port probing scan
White
Version: 1.0
Note: For example MySQL 3306 here, port to 80 can also
"""
#引入线程和socket的模块
import threadingimport socketrouters=[] #用于保存探测结果lock = threading. Lock () #构造一把锁def main ():     LOCAL_IP = SOCKET.GETHOSTBYNAME_EX ( Socket.gethostname ()) all_threads=[] for ip in Local_ip[2]: for i in range (1,255): array = ip.split (".") #把IP以点号做分割 array[3]= str (i) new_ip = '. Join (Array) #在把列表的内容转换成IP t = threading. Thread (target=check_ip,args= (NEW_IP,)) #构造线程池 t.start () #开启线程 all_threads.append (t ) for t in all_threads: t.join () #保持队列def check_ip (NEW_IP): s = Socket.socket (Socket.af_inet,socket. SOCK_STREAM) #创建套接字 s.settimeout (1) port = 3306     RESULT = S.CONNECT_EX ((new_ip,port)) if result ==0: lock.acquire () # get lock print (New_ip.ljust (), ' Port %s is open ' %port) lock.release () # lock release print (' Search for router,please wait ... ') if __name__ == ' __main__ ': main ()
Python Scan LAN Port