如何使用 echo "*" 读取密码在Python控制台程序中?

发布于 2024-12-10 18:25:06 字数 158 浏览 0 评论 0 原文

我正在 Windows 下使用 Python 编写控制台程序。
用户需要登录才能使用该程序,当他输入密码时,我希望它们回显为“*”,同时我可以获取用户输入的内容。
我在标准库中找到了一个名为 getpass 的模块,但是当您输入时它不会回显任何内容(类似 linux)。
谢谢。

I'm writing a console program with Python under Windows.
The user need to login to use the program, when he input his password, I'd like they to be echoed as "*", while I can get what the user input.
I found in the standard library a module called getpass, but it will not echo anything when you input(linux like).
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的那么颓废 2024-12-17 18:25:06

getpass 模块是用 Python 编写的。您可以轻松修改它来执行此操作。事实上,这里是 getpass.win_getpass() 的修改版本,您可以将其粘贴到代码中:

import sys

def win_getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    import msvcrt
    for c in prompt:
        msvcrt.putch(c)
    pw = ""
    while 1:
        c = msvcrt.getch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
            msvcrt.putch('\b')
        else:
            pw = pw + c
            msvcrt.putch("*")
    msvcrt.putch('\r')
    msvcrt.putch('\n')
    return pw

但是,您可能需要重新考虑这一点。 Linux方式更好;即使只是知道密码中的字符数,对于想要破解密码的人来说也是一个重要的提示。

The getpass module is written in Python. You could easily modify it to do this. In fact, here is a modified version of getpass.win_getpass() that you could just paste into your code:

import sys

def win_getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    import msvcrt
    for c in prompt:
        msvcrt.putch(c)
    pw = ""
    while 1:
        c = msvcrt.getch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
            msvcrt.putch('\b')
        else:
            pw = pw + c
            msvcrt.putch("*")
    msvcrt.putch('\r')
    msvcrt.putch('\n')
    return pw

You might want to reconsider this, however. The Linux way is better; even just knowing the number of characters in a password is a significant hint to someone who wants to crack it.

累赘 2024-12-17 18:25:06

kindall的答案很接近,但它存在退格键不擦除星号以及退格键能够返回到输入提示之外的问题。

尝试:

def win_getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)
    import msvcrt
    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            if pw == '':
                pass
            else:
                pw = pw[:-1]
                msvcrt.putwch('\b')
                msvcrt.putwch(" ")
                msvcrt.putwch('\b')
        else:
            pw = pw + c
            msvcrt.putwch("*")
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw

注意 mscvrt.putwch 不适用于 python 2.x,您需要使用 mscvrt.putch 代替。

kindall's answer is close, but it has issues with backspace not erasing the asterisks, as well as backspace being able to go back beyond the input prompt.

Try:

def win_getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)
    import msvcrt
    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            if pw == '':
                pass
            else:
                pw = pw[:-1]
                msvcrt.putwch('\b')
                msvcrt.putwch(" ")
                msvcrt.putwch('\b')
        else:
            pw = pw + c
            msvcrt.putwch("*")
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw

Note mscvrt.putwch does not work with python 2.x, you need to use mscvrt.putch instead.

筑梦 2024-12-17 18:25:06

您可以使用 getpass 模块。 这并不完全正确回答这个问题是因为 getpass 函数除了提示之外不会向控制台输出任何内容。这样做的原因是它是一个额外的安全层。如果有人在监视您,他们将无法计算出您的密码有多长。

以下是如何使用它的示例:

from getpass import getpass
getpass('Enter your password: ')

此示例将显示“输入您的密码:”,然后您可以输入您的密码。

You can use the getpass module. This doesn't exactly answer the question because the getpass function doesn't output anything to the console except for the prompt. The reason for this is that it's an extra layer of security. If someone is watching over your shoulder, they won't be able to figure out how long your password is.

Here's an example of how to use it:

from getpass import getpass
getpass('Enter your password: ')

This example will display "Enter your password: " and then you can type in your password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文