2016-08-24 46 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讓我的一天大聲笑 –