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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7838564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:18:17  来源:igfitidea点击:

How to read password with echo "*" in Python console program?

pythonwindows

提问by wong2

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.

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

采纳答案by kindall

The getpassmodule 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:

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 == '
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 == '
from getpass import getpass
getpass('Enter your password: ')
3': 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
3': 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.

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

回答by Joseph Webber

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.

kindall 的回答很接近,但它存在退格不能擦除星号的问题,以及退格可以返回超出输入提示的问题。

Try:

尝试:

##代码##

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

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

回答by Joseph Webber

You can use the getpassmodule.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.

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

Here's an example of how to use it:

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

##代码##

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

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