如何在Python中修复“使用线程使用TypeError”

时间:2019-05-05 01:21:04

标签: python multithreading

我的第一个ZipCracker可以工作,但是它只是检查了介于0到2000之间的密码。我希望我的ZipCracker可以检查每个组合并使用线程对其进行编程,但现在不起作用。

我现在正在学习Python的编程语言。我想稍微练习一下。我决定编写一些ZipCracker。

# -*- coding: UTF-8 -*-
from threading import Thread
import zipfile
import string
import itertools

class ZipCracker():

        def __init__(self, filename): 
        self.filename = filename
        self.zipfile = zipfile.ZipFile(filename)
        self.targetdirectory = filename[:-4]

    def test_password(self, password):
        try:
            self.zipfile.extractall(path=self.targetdirectory, pwd =  str.encode(password))
            return True
        except Exception as e:
            return False

    def brute_force(self):

        zipFile = self.targetdirectory
        myLetters = string.ascii_letters + string.digits + string.punctuation
        for i in range(3, 10):
            for j in map(''.join, itertools.product(myLetters, repeat=i)):
                t = Thread(target=ZipCracker.test_password, args=(zipFile, j,))
                password = str(j)
                print password
                t.start()

            if self.ZipCracker.test_password(password):
                print("Erfolg: " + password)
                return password

        return None

我希望输出是应该显示所有可能性,并且当正确的密码存在时它应该停止,但是实际输出是以下错误:

Exception in thread Thread-6152:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: unbound method test_password() must be called with ZipCracker instance as first argument (got str instance instead)

0 个答案:

没有答案