【发布时间】:2021-08-10 06:28:16
【问题描述】:
所以首先代码中没有错误,但问题是我认为它会很快但没有多处理需要相同的时间那么有没有办法让它更快?所以我尝试了多处理和线程,但看不到好的结果这是一个端口扫描工具,我正在努力快速制作,也提前感谢。
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 = '\033[94m'
green = '\033[92m'
red = '\033[93m'
underline = '\033[4m'
reset = '\033[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}----------------------------------------------------------------\n\n{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)'''
【问题讨论】:
标签: python python-3.x multithreading multiprocessing