如何隐藏密码并显示';*';在Python下

如何隐藏密码并显示';*';在Python下,python,Python,我想写一个小项目,它需要你输入你的id和passwd,但是我需要一个函数将passwd替换为“*”,当你输入passwd时,我只知道raw_input()可以输入一些东西,所以我无法解决这个问题。如何编写函数?尝试以下操作: import getpass pw = getpass.getpass() 如果所有这些都失败了,您可以修改getpass库(不幸的是,您不能将其子类化) e、 g.windows()的代码: def win_getpass(prompt='Password:',stre

我想写一个小项目,它需要你输入你的id和passwd,但是我需要一个函数将passwd替换为“*”,当你输入passwd时,我只知道raw_input()可以输入一些东西,所以我无法解决这个问题。如何编写函数?

尝试以下操作:

import getpass
pw = getpass.getpass()

如果所有这些都失败了,您可以修改getpass库(不幸的是,您不能将其子类化)

e、 g.windows()的代码:

def win_getpass(prompt='Password:',stream=None):
“”“使用Windows getch(),在echo关闭的情况下提示输入密码。”
如果sys.stdin不是sys.\u stdin\u:
返回回退\u getpass(提示,流)
导入msvcrt
随机输入
对于提示中的c:
msvcrt.putwch(c)
pw=“”
而1:
c=msvcrt.getwch()
如果c='\r'或c='\n':
打破
如果c=='\003':
升起键盘中断
如果c=='\b':
pw=pw[:-1]
其他:
pw=pw+c
stars=random.randint(1,3)
对于范围内的i(星):

msvcrt.putwch('*')#在接受密码时,我在寻找使python打印'*'而不是空白的方法后发现了这篇文章,我发现SiHa的答案非常有用,但如果我们按backspace,它不会擦除已经打印的'*',所以我再次看到另一篇文章:,组合代码:

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
import random
for c in prompt:
    msvcrt.putch(c)
pw = ""
while 1:
    c = msvcrt.getwch()
    if c == '\r' or c == '\n':
        break
    elif c == '\003':
        raise KeyboardInterrupt
    elif c == '\b':
        if pw != '': # If password field is empty then doesnt get executed
            pw = pw[:-1]
            msvcrt.putwch(u'\x08')
            msvcrt.putch(' ')
            msvcrt.putwch(u'\x08')
    else:
        pw = pw + c
        msvcrt.putch('*') #<= This line added
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw
def win_getpass(prompt='Password:',stream=None):
“”“使用Windows getch(),在echo关闭的情况下提示输入密码。”
如果sys.stdin不是sys.\u stdin\u:
返回回退\u getpass(提示,流)
导入msvcrt
随机输入
对于提示中的c:
msvcrt.putch(c)
pw=“”
而1:
c=msvcrt.getwch()
如果c='\r'或c='\n':
打破
elif c=='\003':
升起键盘中断
elif c=='\b':
如果pw!='':#如果密码字段为空,则不会执行
pw=pw[:-1]
msvcrt.putwch(u'\x08')
msvcrt.putch(“”)
msvcrt.putwch(u'\x08')
其他:
pw=pw+c

msvcrt.putch('*')#但是如何在我输入pwd时显示'*',而不是什么都不显示。我不认为有一个参数可以使getpass输出星号,但是如果确实需要,您可以随时实现自己的。看看getpass.py源代码,并以此作为起点。当您不显示该密码的字符数时,这实际上是一种安全改进。但是,您可以查看前面的答案,以便用*回音输入。谢谢你,它能工作,但有点问题,第7、19、20、21行,它们不能是str,在我运行它之后,dos告诉我。有趣的是,
msvcrt.putwch()
也给了我
类型错误:必须是不能转换原始缓冲区,而不是str
。您是否在使用Python2?我已经补充了上面的答案。如果您进行了建议的更改,它应该与Python 2.7一起使用。
msvcrt.putwch()
需要unicode字符串。在Python2中,您需要执行
msvcrt.putwch(u'\r')
而不是
msvcrt.putwch('\r')
人们已经这样做了。。。stackoverflow.com/a/67327327/701532@lanAuld的可能副本,哦,但我需要在您输入密码时显示“*”,而不是仅隐藏。
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
import random
for c in prompt:
    msvcrt.putch(c)
pw = ""
while 1:
    c = msvcrt.getwch()
    if c == '\r' or c == '\n':
        break
    elif c == '\003':
        raise KeyboardInterrupt
    elif c == '\b':
        if pw != '': # If password field is empty then doesnt get executed
            pw = pw[:-1]
            msvcrt.putwch(u'\x08')
            msvcrt.putch(' ')
            msvcrt.putwch(u'\x08')
    else:
        pw = pw + c
        msvcrt.putch('*') #<= This line added
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw