在Python中线程化密码破解程序

我正在努力用Python编写线程密码破解程序。当我尝试运行它时,它会显示一个错误。我不知道将所需的位置参数传递给我的crack()函数。

您在这里找到了我到目前为止得到的代码:

#/usr/bin/python3.7
from itertools import product
import threading
from queue import Queue
from string import ascii_letters,digits

print_lock = threading.Lock()

password = "test"
dictionary = ascii_letters + digits

def crack (dictionary,x):
    for x in range (1,len(dictionary) + 1):
        seq = product(dictionary,repeat=x)
        for item in seq:
            if (''.join(item) == password):
                with print_lock:
                    print("Password found: ",password)

q = Queue()

def threader ():
    while True:
        worker = q.get()
        crack(worker)
        q.task_done()

for x in range (20):
    t = threading.Thread(target = threader)
    t.daemon = True
    t.start()

for worker in range (1,len(dictionary)):
    q.put(worker)

q.join()
rabbitcf 回答:在Python中线程化密码破解程序

这是工作版本:

from itertools import product
import threading
from queue import Queue
from string import ascii_letters,digits

print_lock = threading.Lock()

password = "test"
chracter_list = ascii_letters + digits

def crack (x):
    for x in range (1,len(chracter_list) + 1):
        seq = product(chracter_list,repeat=x)
        for item in seq:
            if (''.join(item) == password):
                with print_lock:
                    print("Password found: ",password)

q = Queue()

def threader ():
    while True:
        worker = q.get()
        crack(worker)
        q.task_done()

for x in range (20):
    t = threading.Thread(target = threader)
    t.daemon = True
    t.start()

for worker in chracter_list:
    q.put(worker)

q.join()

请注意,线程并不会使您的代码更快,也会使您的代码慢一些。如果您想使速度更快,则应使用多重处理。

本文链接:https://www.f2er.com/3090232.html

大家都在问