TCP连接扫描,基于socket,使用threading实现多线程,并控制进程数量。
注:threads_limit 变量视主机情况而定,定义太多会出现如下错误:
代码:
import socket import threading #change these vars. start_port = 20 end_port = 1000 host = ‘8.8.8.8‘ threads_limit = 600 open_port = [] mylock = threading.Lock() def scan_port(port): global mylock mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if (mysock.connect_ex((host, port)) == 0): mylock.acquire() open_port.append(port) mylock.release() for will_scan_port in range(start_port, end_port+1): mythread = threading.Thread(target = scan_port, args =(will_scan_port,)) mythread.start() #limit thread number while (len(threading.enumerate()) >= 600): pass print open_port
时间: 2024-10-11 18:10:21