在同一行上打印更新的计数器

2024-09-16 20:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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替换,等等,直到密码散列被破解。就像一个加载条,只需要迭代到目前为止的数字


Tags: inputmaincounternotpasspasswordhashword
2条回答

使用字符串连接:

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

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

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)打印出来,因此您需要手动刷新

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

相关问题 更多 >