如何修复Python中的“TypeError using Threads”

2024-04-19 19:11:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的第一个ZipCracker工作了,但它只检查了0到2000之间的密码。我想我的ZipCracker可以检查每一个组合,并用线程编程,现在它不工作了

我现在学习编程语言Python。我想练习一下。我决定编一个小程序

# -*- 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)

Tags: intestimportselfstringreturndefpassword