我正在尝试在我的代码中包含多处理支持。它现在工作得非常快,但字典迭代不会在找到密码时停止。我试着重置字典,但没用。
import PyPDF2
import time
import os
import sys
from joblib import Parallel, delayed
import multiprocessing
def iterDict(password):
password1 = password.lower()
if pdfReader.decrypt(password) or pdfReader.decrypt(password1):
if pdfReader.decrypt(password1):
password = password1
print(f'Password: {password}')
passwords = [1] # list reset don't help to stop dictionary iteration
return passwords
else:
return False
def decrypt():
if pdfReader.isEncrypted:
print(f'Working... {time.asctime()}')
start = time.time()
numCores = multiprocessing.cpu_count()
if Parallel(n_jobs=numCores)(
delayed(iterDict)(password) for password in passwords):
end = time.time()
hours = int((end - start) / 3600)
minutes = int((end - start - hours * 3600) / 60)
secondes = int(end - start - (hours * 3600) - (minutes * 60))
print(f'{pdf} has been decrypted in {hours}H:{minutes}M:{secondes}S!')
else:
print(f'{pdf} hasn\'t been decrypted... Maybe need a better dictionary?')
else:
print(f'{pdf} isn\'t encrypted')
if len(sys.argv) == 3:
dictionary, pdf = sys.argv[1], sys.argv[2]
if os.path.isfile(dictionary) and dictionary.endswith('.txt'):
if os.path.isfile(pdf) and pdf.endswith('.pdf'):
global passwords
passwords = open(dictionary).read().split('\n')
pdfReader = PyPDF2.PdfFileReader(pdf)
decrypt()
else:
print('Invalid path to pdf or pdf file')
else:
print('Invalid path to dictionary or dictionary file')
else:
print('Please enter arguments as example:\
\ndictionaryName.txt pdfName.pdf')有什么建议吗?非常感谢!
发布于 2021-03-20 00:23:49
所以我自己想出了答案...我在decrypt()中更改了Parallel(n_jobs=numCores,timeout=1),并在iterDict()中添加了time.sleep(2)。
下面是我的最终代码:
import PyPDF2
import time
import os
import sys
from joblib import Parallel, delayed
import multiprocessing
def iterDict(password):
password1 = password.lower()
if pdfReader.decrypt(password) or pdfReader.decrypt(password1):
if pdfReader.decrypt(password1):
password = password1
print(f'Password: {password}')
time.sleep(2)
return True
else:
return False
def decrypt():
if pdfReader.isEncrypted:
print(f'Working... {time.asctime()}')
start = time.time()
numCores = multiprocessing.cpu_count()
try:
Parallel(n_jobs=numCores, timeout=1)(
delayed(iterDict)(password) for password in passwords)
except multiprocessing.context.TimeoutError:
wTime = time.strftime('%H:%M:%S', time.gmtime(time.time() - start))
print(f'{pdf} has been decrypted in {wTime}!')
else:
print(f'{pdf} hasn\'t been decrypted... Maybe need a better dictionary?')
else:
print(f'{pdf} isn\'t encrypted')
if len(sys.argv) == 3:
dictionary, pdf = sys.argv[1], sys.argv[2]
if os.path.isfile(dictionary) and dictionary.endswith('.txt'):
if os.path.isfile(pdf) and pdf.endswith('.pdf'):
passwords = open(dictionary).read().split('\n')
pdfReader = PyPDF2.PdfFileReader(pdf)
decrypt()
else:
print('Invalid path to pdf or pdf file')
else:
print('Invalid path to dictionary or dictionary file')
else:
print('Please enter arguments as example:\
\ndictionaryName.txt pdfName.pdf')但也许有人有更好的解决方案或任何建议来改进我的代码?谢谢!
https://stackoverflow.com/questions/66705871
复制相似问题