Python port scan with multithreading + thread-safe Queue +thread class implementation

Source: Internet
Author: User
Tags thread class

    • 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.