我正在使用此代码来破坏简单的密码mycubana,但是我想“暂停” itertools循环,以便在一段时间之后,我可以从上次保存的itertools.product
值开始恢复循环。有没有办法改变很多代码呢?
代码:
import string
from itertools import chain, product
def bruteforce(charset, minlenght, maxlenght):
return(''.join(candidate)
for candidate in chain.from_iterable(product(charset, repeat = i)
for i in range(minlenght, maxlenght)))
contador = 0
for attempt in bruteforce(string.ascii_lowercase, 8, 9):
codigo = 'mycubana'
contador+=1
if attempt==codigo:
print(attempt)
contador=0
是的,实际上非常简单。使用您的环境和bruteforce
的定义,以下代码将对生成的序列执行两次,每次运行10个候选对象:
bf26_8 = bruteforce(string.ascii_lowercase, 8, 9)
count = 0
for c in bf26_8:
count += 1
print(c)
if count == 10:
break
print("======== PAUSED ========")
for c in bf26_8:
count += 1
print(c)
if count == 20:
break
“技巧”是将bruteforce
的结果存储在变量中。该结果为generator,因此,如果您对其进行迭代,但又不穷尽(即中断迭代循环),则在您再次开始迭代时,它将为您提供延续值。