如何在输入密码时将密码转换为星号? [英] How do I convert a password into asterisks while it is being entered?

查看:48
本文介绍了如何在输入密码时将密码转换为星号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 中是否有一种方法可以将用户输入的字符转换为星号,就像在许多网站上可以看到的那样?

例如,如果电子邮件用户被要求登录他们的帐户,在输入他们的密码时,它不会显示为字符,而是显示为 * 在每个笔画之后没有任何时间滞后.

如果实际密码是KermitTheFrog,输入时会显示为****************.

解决方案

如果您想要一个适用于 Windows/macOS/Linux 和 Python 2 & 的解决方案3、可以安装stdiomask模块:

pip install stdiomask

getpass.getpass()(在 Python 标准库中)不同,stdiomask 模块可以在您键入时显示 *** 掩码字符.

示例用法:

<预><代码>>>>stdiomask.getpass()密码: *********'旗鱼'>>>stdiomask.getpass(mask='X') # 更改掩码字符.密码:XXXXXXXXX'旗鱼'>>>stdiomask.getpass(prompt='PW: ', mask='*') # 更改提示.密码:********'旗鱼'>>>stdiomask.getpass(mask='') # 不显示任何内容.密码:'旗鱼'

不幸的是,这个模块与 Python 的内置 getpass 模块一样,在 IDLE 或 Jupyter Notebook 中不起作用.

更多详情请访问 https://pypi.org/project/stdiomask/

Is there a way in Python to convert characters as they are being entered by the user to asterisks, like it can be seen on many websites?

For example, if an email user was asked to sign in to their account, while typing in their password, it wouldn't appear as characters but rather as * after each individual stroke without any time lag.

If the actual password was KermitTheFrog, it would appear as ************* when typed in.

解决方案

If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the stdiomask module:

pip install stdiomask

Unlike getpass.getpass() (which is in the Python Standard Library), the stdiomask module can display *** mask characters as you type.

Example usage:

>>> stdiomask.getpass()
Password: *********
'swordfish'
>>> stdiomask.getpass(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> stdiomask.getpass(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> stdiomask.getpass(mask='') # Don't display anything.
Password:
'swordfish'

Unfortunately this module, like Python's built-in getpass module, doesn't work in IDLE or Jupyter Notebook.

More details at https://pypi.org/project/stdiomask/

这篇关于如何在输入密码时将密码转换为星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆