2016-08-24 42 views
2

我对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)) 
     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 

所以我的问题是如何才能得到进度条输出,而obtain_password方法运行?我是否需要稍微改变一下进度条方法?

+1

你可以尝试有进度条和另外一个一个线程用于'gets_password'函数。 http://www.tutorialspoint.com/python/python_multithreading.htm或者你可以使'gets_password'函数稍微画一下进度条。 – grael

+1

@grael在这一点上,对于我来说这似乎有点进步,我喜欢它,谢谢。 –

回答

2

你试图做的事情是行不通的,你必须记住你只有一个线程。

你可以做什么,是得到你的单词表中的行数,并做数学。顺便说一句,它肯定比定时器更精确。

我没有测试的代码,虽然与这些方针的东西,你会得到你想要什么:

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提出,把他们捉住正常。 抓住这样的一切:except:不是一个好的做法。

+0

你能否偶然向我展示一个通过线程来做的例子? –

+1

哈哈,我可以,但是你不会学习,你的黑客技能不会升高。亲自给它一个镜头,如果你有问题,发布一个新的问题:-) –

+0

顺便说一下,使用进度条的线程是无用的imho。虽然如果“解压缩测试部分”需要时间,那么线程可能会很有趣。 –

2

有办法做你的愿望。

  1. 让您的密码,黑客在一段时间更新一次进度

    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() 
    
  2. 使用线程

    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 
    
+0

我真的不明白线程实例。你能解释一下怎么回事吗? –

+1

这是什么巫师? –

+0

@Loïc让我的一天大声笑 –