python - 同时运行多处理和线程
问题描述
所以首先代码中没有错误,但问题是我认为它会很快但没有多处理需要相同的时间那么有没有办法让它更快?所以我尝试了多处理和线程,但看不到一个好的结果,这是一个端口扫描工具,我正在努力快速制作,也提前感谢。
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)'''
解决方案
请注意,多处理用于 CPU 繁重的计算,其中多线程(通常)用于 I/O 绑定任务,即从数据库等获取数据时。
老实说,我不知道你的代码在做什么,但它似乎做了很多非 CPU 密集型扫描/东西,因此我可能只坚持多线程
推荐阅读
- python - 将日期时间列拆分为熊猫中的日期和时间列
- python - 使用 python 将 .txt 文件转换为 .csv 文件
- javascript - 将一种形式更改为另一种材质ui到antd
- wordpress - 将 Javascript 添加到 Wordpress 帖子正文
- python - Python/Pyinstaller 未正确安装
- python - 希望输出返回 S 曲线
- c++ - 将非 const char * 传递给函数的 const char *& 参数
- sql - 参数 * 只能用于 COUNT(*)
- apache - 如何将非 php 扩展 url 重定向到 php 扩展 url htaccess?
- python - 如何在 Python 中重载构造函数(而不是初始化程序)