#!/usr/bin/env python3#coding:utf-8'''
@author:oxff
@github:https://github.com/oxff644
'''#進程池掃描主機端口,進程池技術可以一次創建多個子進程,適合於子進程數量事先預知的情況from multiprocessing import Pool
import os
import socket
defscan_port(ports=0):
s =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(1)for port inrange(ports,ports+4096):
result =s.connect_ex((ip,port))#print(result)if result==0:print('I am process %d,port %d is openned!'%(os.getpid(),port))
s.close()
ip ='192.168.134.128'
p =Pool(16)for k inrange(16):
p.apply_async(scan_port,args=(k*4096,))#每個子進程要掃描的開始端口是k*4096
p.close()
p.join()print('all subprocesses had finished!')
#!/usr/bin/env python3#coding:utf-8'''
@author:oxff
@github:https://github.com/oxff644
'''#線程池掃描主機端口import threadpool
import socket
defscan_port(num):
s =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(1)
ports =num*4096
thread_name ='thread'+str(num)for port inrange(ports,ports+4096):
result =s.connect_ex((ip,port))if result ==0:print('I am %s,port %d is openned!'%(thread_name,port))
s.close()
ip =input('enter a Ip address: ').strip()
p =threadpool.ThreadPool(16)
num_list =list(range(16))
tasks =threadpool.makeRequests(scan_port,num_list)for task in tasks:
p.putRequest(task)
p.wait()print("All thread had finished!")