我决定写一个蛮力的功能,以向人们展示密码有多脆弱。现在,我可以向他们展示查找密码的列表,但是我该如何告诉他们花了多长时间?这是代码:

#!/usr/bin/python
import itertools

lower_a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

alllet = []
alllet = lower_a + upper_a# + num
pwd = raw_input("What pwd?\t\t")

try:
        for r in range(1, len(pwd)+1):
                for s in itertools.product(alllet, repeat=r):
                        print ''.join(s)
                        if ''.join(s) == pwd:
                                raise NameError()

except KeyboardInterrupt:
        print "Hey! You stopped me!"

except NameError:
        print "DONE! CRACKED!"
        print "\n\nPassword is:\t" + ''.join(s) + "\n\n"


有帮助吗?

解决方案

首先,密码非常安全,需要很多天才能使用蛮力攻击找到一个。

但是,如果您愿意,可以使用以下以下内容:

import time

start_time = time.time()

# Your code here

stop_time = time.time()
print "Running time in sec:", stop_time - start_time

其他提示

您需要的是 timeit 模块。使用 timeit.Timer, ,您可以测量代码的速度。

这里 是一个很好的在线教程。

希望这可以帮助

及时多长时间?

from time import time 最重要的是,在程序开始时 start = time(). 。以名称为“零件” print time() - start

请更具体

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top