import socket,threadpool
ports=[]
def scan_poort(port):
ip='192.168.1.131'
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# socket.socket 创建socket 实例
# AF_INET 意思是基于网络
# SOCK_STREAM 意思是基于TCP的
# SOCK_DGRAM是基于UDP的
try:
statu=s.connect_ex((ip,port))
if statu==0:
print(port,'is open')
except:
pass
def get_port():
global ports
for p in range(65536):
ports.append(p)
get_port()
pool=threadpool.ThreadPool(100)
# 设置线程,创建一个线程池
reqs=threadpool.makeRequests(scan_poort,ports)
# 把ports传入scan_poort函数中去执行,生成所需要的所有线程
[pool.putRequest(req) for req in reqs]
# 等价于
# for req in reqs
# pool.putRequest(req)
pool.wait()
# 等待其他线程结束