我有一些旧的RAR文件,我知道我以前使用的密码的一般格式,所以我生成了一个以各种方式组合这些单词的字典文件。字典文件是500 MB,我写了一个python程序来使用字典。
问题是该程序已经运行一整夜,只占了1%。我现在已经将字典文件分开了,我现在正在运行程序的多个实例,但是每个实例都是跑得很慢。
我很想知道如何提高速度。我对python很陌生,我的代码很明显(但我真的很喜欢python)
import itertools
import sys, traceback
import os
with open('dictionary.txt') as infile:
words = [line.strip() for line in infile]
for word1 in itertools.product(words, repeat=1):
fword = word1[0]
print "Attempting: " + fword
# open unrar and test password
output = os.popen("unrar.exe x protected.rar -p" + fword)
outstring = output.read()
slng = len(outstring)
# unrar.exe returns "All OK" for correct password
validate = outstring.find("All OK", 0, slng)
if validate != -1:
print "Found password: " + fword
with open('correct_password.txt', 'w') as outfile:
outfile.write(fword)
sys.exit(0)
else:
# continue searching
print "invalid password"
raw_input("Password was not in this dictionary!")
示例 dictionary.txt
FableLand
FableWay
FableTree
FableSpirit
FableApple