【问题标题】:How to hide passwd and show ' * ' under Python如何在Python下隐藏密码并显示'*'
【发布时间】:2015-09-26 00:54:53
【问题描述】:

我想写一个小项目,它需要你输入你的id和passwd,但是我需要一个函数在你输入passwd时用'*'替换passwd,我只知道raw_input()可以输入一些东西,所以我无法解决问题。函数怎么写?

【问题讨论】:

标签: python


【解决方案1】:

试试这个:

import getpass
pw = getpass.getpass()

【讨论】:

  • 但是当我输入密码时如何显示'*'而不是什么都不显示。
  • 我不认为有一个参数可以让 getpass 输出星号,但如果你真的需要,你总是可以实现你自己的。查看 getpass.py 源并将其用作起点。
  • 当您不显示该密码的字符数时,这实际上是一种安全改进。但是,您可以查看这个先前的答案,以使用 * 回显输入。 stackoverflow.com/a/10991348/5066845
【解决方案2】:

如果一切都失败了,您可以修改 getpass 库(很遗憾,您不能对其进行子类化)。

例如windows的代码(source):

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.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
        else:
            pw = pw + c
            stars = random.randint(1,3)
            for i in range(stars):
                msvcrt.putwch('*') #<= This line added
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw

应该为输入的每个字符打印一个'*'

编辑:

getpass() 被声明与 Python 2 不兼容,而在 Python 2 上(至少在我的机器上),putwch() 给出了TypeError: must be cannot convert raw buffers, not str

这可以通过改变来解决:

msvcrt.putwch(c)msvcrt.putwch(unicode(c))

msvcrt.putwch('str')msvcrt.putwch(u'str')

如果您不需要处理 unicode,或者只需将 putwch() 替换为 putch()

编辑2:

我添加了一个随机元素,这样每次按键都会打印 1-3 颗星

【讨论】:

  • 谢谢你,它有效,但有问题,第 7,19,20,21 行,它们不能是 str,在我运行它之后,dos 告诉我。
  • 有趣的是,msvcrt.putwch() 也给了我TypeError: must be cannot convert raw buffers, not str。您是否正在使用 Python 2?
  • 我已经添加到上面的答案中。如果您进行了建议的更改,它应该适用于 Python 2.7。
  • msvcrt.putwch() 需要一个 unicode 字符串。在 python 2 你需要做 msvcrt.putwch(u'\r') 而不是 msvcrt.putwch('\r')
  • 人们已经做到了... stackoverflow.com/a/67327327/701532
【解决方案3】:

我在寻找在接受密码时让 python 打印“*”而不是空格的方法后发现了这篇文章,我发现 SiHa 的答案真的很有帮助,但它并没有删除“ *' 如果我们按退格键已经打印,所以我又遇到了另一个帖子:How to have password echoed as asterisks,组合代码:

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

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 2018-08-13
    • 2021-12-01
    • 2018-01-15
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多