Python 为输出使用进度条
我对Python非常陌生,并创建了一个小密码破解程序,它使用暴力攻击,我试图在程序运行时让进度条输出,以下是我目前所拥有的:Python 为输出使用进度条,python,loops,Python,Loops,我对Python非常陌生,并创建了一个小密码破解程序,它使用暴力攻击,我试图在程序运行时让进度条输出,以下是我目前所拥有的: import zipfile import sys import time def progress_bar(sleep_time): for i in range(101): time.sleep(sleep_time) sys.stdout.write("\r[{0}] {1}%".format('#'*(i/10), i)
import zipfile
import sys
import time
def progress_bar(sleep_time):
for i in range(101):
time.sleep(sleep_time)
sys.stdout.write("\r[{0}] {1}%".format('#'*(i/10), i))
sys.stdout.flush()
def obtain_password(path_to_zip_file):
password = None
zip_file = zipfile.ZipFile(path_to_zip_file)
with open('wordlist.txt', 'r') as dict:
for line in dict.readlines():
possible = line.strip("\n")
try:
zip_file.extractall(pwd=possible)
password = "Password found {}".format(possible)
except:
pass
return password
因此,我的问题是,当
acquire\u password
方法运行时,如何获得要输出的进度条?我需要改变一下进度条的方法吗 你想做的事行不通,你必须记住你只有一个线程
但你可以做的是,在你的单词列表中得到行数,然后做数学运算。顺便说一句,它肯定比计时器精确得多
我没有对代码进行测试,但通过以下方法,您将得到您想要的:
import zipfile
import sys
import time
def obtain_password(path_to_zip_file):
password = None
zip_file = zipfile.ZipFile(path_to_zip_file)
with open('wordlist.txt', 'r') as f:
lines = f.readlines()
total = len(lines) # get number of lines
current = 0
for line in lines:
current += 1
if current % 1000 == 0: # every 1000 lines, shows the progress
print('%.2f %%' % float(current / total * 100))
possible = line.strip("\n")
try:
zip_file.extractall(pwd=possible)
#password = "Password found {}".format(possible)
print(possible)
sys.exit()
except:
pass
另外,我建议您获取extractall
引发的异常,并正确捕获它们。
捕捉这样的一切:
除了:
不是一个好的做法。有很多方法可以做你想做的事
import time
# Stores the time between updates in seconds.
time_between_updates = 10
last_update = 0
def your_expensive_operation():
for i in range(10000000):
time.sleep(1) # Emulate an expensive operation
if time.time() - last_update > time_between_updates:
print("\r" + (int(i/10000000.0 * 79) * "#"), end='')
your_expensive_operation()
import time
import threading
# Stores your current position in percent.
current_position = 0
done = False
def paint_thread():
while not done:
print("\r" + (int(current_position * 79) * "#"), end='')
# Make it update once a second.
time.sleep(1)
thread = threading.Thread(target=paint_thread)
thread.start()
for i in range(10000000):
time.sleep(1) # Emulate an expensive operation
current_position = i/10000000.0
done = True
您可以尝试让一个线程用于进度条,另一个线程用于
acquire\u password
函数。或者你可以让acquisite\u password
函数偶尔画一次进度条。@grael这对我来说似乎有点高级,哈哈,不过我喜欢,谢谢。你能给我一个通过线程实现的例子吗?哈哈,我可以,但你不会学,你的黑客技能也不会提高。给它一个自己的镜头,如果你有一个问题,张贴一个新的问题:-)顺便说一句,使用一个线程的进度栏是无用的imho。虽然如果“解压测试部分”需要时间,那么线程可能会很有趣。哈哈,好吧,我会看看我能想出什么。谢谢,我真的不理解线程实例。你能解释一下发生了什么吗?这是什么分类法?线程使一次运行多段代码成为可能。stackoverflow的文档页面应该为您提供一些代码示例。不幸的是,我在回家的火车上,所以我无法发送链接。