首页 > 解决方案 > 同时运行多处理和线程

问题描述

所以首先代码中没有错误,但问题是我认为它会很快但没有多处理需要相同的时间那么有没有办法让它更快?所以我尝试了多处理和线程,但看不到一个好的结果,这是一个端口扫描工具,我正在努力快速制作,也提前感谢。

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)'''

标签: pythonpython-3.xmultithreadingmultiprocessing

解决方案


请注意,多处理用于 CPU 繁重的计算,其中多线程(通常)用于 I/O 绑定任务,即从数据库等获取数据时。

老实说,我不知道你的代码在做什么,但它似乎做了很多非 CPU 密集型扫描/东西,因此我可能只坚持多线程


推荐阅读