- Scan port data storage with thread-safe queue queues
- Scan ports with multithreading
- Implementing program organization with the thread class
#Coding:utf-8ImportSYSImportSocketImportSYSImportRe fromThreadingImportThread fromQueueImportQueueclassScanport (Thread):def __init__(self, target_ip, Target_port, Q): Super (Scanport, self).__init__() self.target_ip=target_ip Self.target_port=Target_port self.q=QdefScan_port (self):Try: S=Socket.socket (Socket.af_inet,socket. Sock_stream) S.connect ((Self.target_ip, Self.target_port)) Q.put (str (self.target_port)+'is opening') S.close ()exceptexception,e:Pass defRun (self): Self.scan_port ()if __name__=='__main__': Port_list= [21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] Target_ip= Sys.argv[1] Q=Queue () portthreadlist= [Scanport (target_ip, Port, Q) forPortinchPort_list] forTinchPortthreadlist:t.setdaemon (True) T.start () forTinchPortthreadlist:t.join ()#End enters the queue, indicating that the scan is overQ.put ('End') whileTrue:result=Q.get ()PrintresultifRe.search (R'End', result): Break
Python port scan with multithreading + thread-safe Queue +thread class implementation