所以第一件事是代码中没有错误,但问题是我认为它会很快,但没有多处理需要相同的时间,所以有没有办法使它更快?所以我尝试多处理和线程在一起,但看不到一个好的结果,这是一个端口扫描工具,我试图使快速,也提前感谢。
import socket
import threading
from queue import Queue
import subprocess
import sys
import os
import multiprocessing as mp
open_ports = []
print_lock = threading.Lock()
host = sys.argv[1]
try:
protocol = sys.argv[2] #User Specified Protocol
except IndexError:
protocol = '-vvv' #Default Prtocol
q = Queue()
# color class
class color:
blue = ' 33[94m'
green = ' 33[92m'
red = ' 33[93m'
underline = ' 33[4m'
reset = ' 33[0m'
# banner
def banner():
os.system('clear')
print(f'{color.blue}----------------------------------------------------------------')
print(r"""
__ ___ __ __________
/ / < /___ _/ //__ / ___/_________ _____
/ / / / __ `/ __ / /__ / ___/ __ `/ __
/ /___/ / /_/ / / / / /___/ / /__/ /_/ / / / /
/_____/_/__, /_/ /_/_//____/___/__,_/_/ /_/
/____/
""")
print('----------------------------------------------------------------')
print('[+] You should have latest Version of NMAP installed')
print('[+] Developed By $witch<Blade#0001')
print(f'[+] Only supports IPv4{color.reset}')
print(f'{color.red}[+] I am not Rusted{color.reset}')
print(f'{color.blue}----------------------------------------------------------------nn{color.reset}')
def portscan(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connecting to the remote host
s.settimeout(1)
try:
con = s.connect((host, port)) # scanning the ports
with print_lock:
print(f'{color.green}[+] port {port} is open{color.reset}')
open_ports.append(port)
con.close() # closing connection
except:
pass
def threader():
while True:
worker = q.get()
portscan(worker)
q.task_done()
def thread1():
for x in range(700):
t = threading.Thread(target = threader)
t.daemon = True
t.start()
for worker in range(1, 32767):
q.put(worker)
def thread2():
for x in range(700):
t = threading.Thread(target = threader)
t.daemon = True
t.start()
for worker in range(32768, 65535):
q.put(worker)
q.join()
def muti_processing():
p1 = mp.Process(target = thread1)
p2 = mp.Process(target = thread2)
p1.start()
p2.start()
if __name__== '__main__':
banner()
muti_processing()
print(f'{color.blue}n n ----------------------------------------------------------')
print('[+] Now All the ports are Scanned')
print(f'[+] Our Nmap Come in Action, [nmap host {protocol} -T4 -p open_ports]')
print(f'----------------------------------------------------------{color.reset}')
separator = ", "
apple = separator.join(map(str, open_ports))
print(color.green)
output = subprocess.check_output(['nmap', host, protocol, '-T4', '-p', apple])
print(output.decode('utf-8'))
print(color.reset)'''
注意,多处理用于CPU繁重的计算,其中多线程(经常)用于I/O绑定任务,例如从数据库获取数据等。
老实说,我不知道你的代码在做什么,但它似乎做了很多非cpu繁重的扫描/东西,因此我可能只是坚持多线程