Getting a hidden password input(获取隐藏密码输入)
问题描述
您知道在 Linux 中,当您尝试一些 Sudo 内容时,它会告诉您输入密码,而当您键入时,终端窗口中没有显示任何内容(未显示密码)?
You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?
有没有办法在 Python 中做到这一点?我正在编写一个需要如此敏感信息的脚本,并希望在我输入时将其隐藏.
Is there a way to do that in Python? I'm working on a script that requires so sensitive info and would like for it to be hidden when I'm typing it.
换句话说,我想在不显示密码的情况下从用户那里获取密码.
In other words, I want to get the password from the user without showing the password.
推荐答案
使用<代码>getpass.getpass():
from getpass import getpass
password = getpass()
一个可选的提示可以作为参数传递;默认为 密码:"
.
An optional prompt can be passed as parameter; the default is "Password: "
.
请注意,此功能需要正确的终端,因此它可以关闭输入字符的回显 - 请参阅 GetPassWarning: Can not control echo on the terminal"从 IDLE 运行时了解更多详情.
Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see "GetPassWarning: Can not control echo on the terminal" when running from IDLE for further details.
这篇关于获取隐藏密码输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取隐藏密码输入


基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01