【问题标题】:Print updated counter on the same line在同一行打印更新的计数器
【发布时间】:2020-12-07 22:05:33
【问题描述】:

我有这个代码:

import hashlib

pass_hash = input("Enter MD5 Hash: ")
wordlist = input("Wordlist name: ")

try:
    pass_file = open(wordlist, 'r')
except FileNotFoundError:
    print("File not found.")
    quit()


def main():
    counter = 0
    print(f"List count: {str(counter)} Type: alphanum")

    for word in pass_file:
        encoded_word = word.encode('utf-8')
        digest = hashlib.md5(encoded_word.strip()).hexdigest()

        counter += 1

        if digest == pass_hash:
            print(f"Password found: {word}")
            break
    else:
        print("Password not found")


main()

我正在尝试打印计数器当前阶段,例如 1 在同一行被 2 替换,然后被 3 等替换,直到密码哈希被破解。就像一个加载条,到目前为止只有数字迭代。

【问题讨论】:

    标签: python passwords counter md5


    【解决方案1】:

    使用字符串连接:

    stringToPrint = "List count: " + str(counter) + " Type: alphanum"
    print(stringToPrint)
    

    【讨论】:

    • 这是输出:List count: 0 Type: alphanum Password found: PolniyPizdec0211
    • 抱歉,我没有看到您希望所有内容都集中在一行中。为此,请运行与我的答案相同的代码,但添加“import os”并使用“os.system('cls')”清除屏幕/控制台(如果使用 Windows)
    【解决方案2】:

    您可以使用回车完成此操作:

    counter = 0
    for word in pass_file:
        sys.stdout.write(f"\rList count: {str(counter)} Type: alphanum")
        sys.stdout.flush()
        counter += 1
    
        encoded_word = word.encode('utf-8')
        digest = hashlib.md5(encoded_word.strip()).hexdigest()
    
        if digest == pass_hash:
            print(f"\nPassword found: {word}")
            break
    

    您需要刷新标准输出流以确保它被写入(通常输出流将等待大量缓冲区或换行符 (\n) 打印出来,因此您需要手动刷新。

    您还需要在找到密码时添加换行符,因为我们在编写计数器时不包含换行符。

    【讨论】:

    • 例如,它只是直接打印数字,而不是从 1 到 500。我需要那个“动画”。
    猜你喜欢
    • 2017-05-30
    • 2014-07-31
    • 2021-12-12
    • 2018-03-20
    • 2012-08-15
    • 1970-01-01
    • 2011-03-26
    • 2011-11-01
    相关资源
    最近更新 更多