Python port Scan (full-connection scan, multi-threaded)

Source: Internet
Author: User

from socket import *import threading #导入线程相关模块lock = threading.Lock()openNum = 0threads = [] #定义线程列表def portScanner(host,port): global openNum try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) lock.acquire() #因为openNum是个全局变量,每个线程不能对openNum 同时操作,只有获得所的线程才可以操作 openNum openNum+=1 print(‘[+] %d open‘ % port) lock.release() #线程对全局变量openNum操作完成后,需要释放所,其他线程才可以继续修改全局变量openNum s.close() except: #如果端口没开,那么就直接pass,不执行其他输出操作。 passdef main(): setdefaulttimeout(1) ports = [20, 21, 22, 23, 80, 111, 3306] #定义要扫描的端口,也可以在for中使用range进行定义,看个人需求,例如 for p in range(1,1024): for p in ports: t = threading.Thread(target=portScanner,args=(‘‘,p)) threads.append(t) t.start() for t in threads: #等待线程列表中的所以线程的执行完毕 t.join() print(‘[*] The scan is complete!‘) print(‘[*] A total of %d open port‘ % (openNum))if __name__ == ‘__main__‘: main()

If you want to calculate how long the scan takes, you can import the time module, and then the program starts running minus the last output time when the program ends running.

Python port Scan (full-connection scan, multi-threaded)

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: 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.