Python中是否有一种方法可以在用户输入星号时转换字符,就像在许多网站上看到的一样?
例如,如果要求电子邮件用户登录他们的帐户,在输入密码时,它不会显示为字符,而是在每次单独笔划后显示为*
,没有任何时间延迟
如果实际密码为KermitTheFrog
,则在输入时会显示为*************
。
答案 0 :(得分:21)
有getpass()
,隐藏用户输入的功能。
checkForWin()
答案 1 :(得分:8)
如果你正在使用Tkinter :(这是Python 2.x.但是,3.x会非常相似)
from Tkinter import Entry, Tk
master = Tk()
Password = Entry(master, bd=5, width=20, show="*")
Password.pack()
master.mainloop()
在shell中,这是不可能的。但是,您可以编写一个函数来存储输入的文本,并在调用时仅报告*的字符串。 Kinda like this,我没有写。我只是用谷歌搜索它。
答案 2 :(得分:3)
您可以这样做:
# if getch module is available, then we implement our own getpass() with asterisks,
# otherwise we just use the plain boring getpass.getpass()
try:
import getch
def getpass(prompt):
"""Replacement for getpass.getpass() which prints asterisks for each character typed"""
print(prompt, end='', flush=True)
buf = ''
while True:
ch = getch.getch()
if ch == '\n':
print('')
break
else:
buf += ch
print('*', end='', flush=True)
return buf
except ImportError:
from getpass import getpass
答案 3 :(得分:1)
您可能需要查看getpass
功能。
提示用户输入密码而不回显。提示用户 使用字符串提示,默认为'密码:'。在Unix上, prompt被写入类文件对象流。流默认为 控制终端(/ dev / tty)或者如果不可用 sys.stderr(在Windows上忽略此参数)。
注意:此模块模仿unix密码提示,但不显示星号。
用法:
import getuser
getuser.getpass()
答案 4 :(得分:1)
对于实际上想要星号出现的任何人,这是对Tigran Aivazian's answer的改进。此版本导入内置的msvcrt.getch
,在按“ Enter / Return”时添加不同行尾的大小写,并包括支持Backspace和Ctrl + C(KeyboardInterrupt)的逻辑:
try:
from msvcrt import getch
def getpass(prompt):
"""Replacement for getpass.getpass() which prints asterisks for each character typed"""
print(prompt, end='', flush=True)
buf = b''
while True:
ch = getch()
if ch in {b'\n', b'\r', b'\r\n'}:
print('')
break
elif ch == b'\x08': # Backspace
buf = buf[:-1]
print(f'\r{(len(prompt)+len(buf)+1)*" "}\r{prompt}{"*" * len(buf)}', end='', flush=True)
elif ch == b'\x03': # Ctrl+C
raise KeyboardInterrupt
else:
buf += ch
print('*', end='', flush=True)
return buf.decode(encoding='utf-8')
except ImportError:
from getpass import getpass
请随时提出任何其他更改或改进方法;我很快就将变更全部归为一体,尤其是使用Backspace逻辑。
答案 5 :(得分:0)
在python中使用getpass
时,没有任何指示显示密码输入。
这可以通过以下简单的方法解决:
只需将链接中提供的‘getpass_ak.py’
模块复制到python的Lib文件夹中即可。
https://starrernet.wixsite.com/analytix/python-coder
使用以下代码:
import getpass_ak
a = (getpass_ak.getpass('password: '))
这会将*添加到您的密码输入中。
答案 6 :(得分:0)
我已经将 @Tigran Aivazian 和 @Ahndwoo 的答案组合为完全有效的解决方案:
{b'\x08', b'\x7f'}: # Backspace
的其他代码Ctrl+C
组合,使用静默返回。 raise KeyboardInterrupt
现在已注释,但可以不加注释以引起错误。# if getch module is available, then we implement our own getpass() with asterisks,
# otherwise we just use the plain boring getpass.getpass()
try:
from getch import getch
def getpass(prompt):
"""Replacement for getpass.getpass() which prints asterisks for each character typed"""
print(prompt, end='', flush=True)
buf = b''
while True:
ch = getch().encode()
if ch in {b'\n', b'\r', b'\r\n'}:
print('')
break
elif ch == b'\x03': # Ctrl+C
# raise KeyboardInterrupt
return ''
elif ch in {b'\x08', b'\x7f'}: # Backspace
buf = buf[:-1]
print(f'\r{(len(prompt)+len(buf)+1)*" "}\r{prompt}{"*" * len(buf)}', end='', flush=True)
else:
buf += ch
print('*', end='', flush=True)
return buf.decode(encoding='utf-8')
except ImportError:
from getpass import getpass
password = getpass('Enter password: ')
print(password)
答案 7 :(得分:0)
如果您想要适用于Windows / macOS / Linux和Python 2&3的解决方案,则可以安装stdiomask
模块:
pip install stdiomask
与getpass.getpass()
(在Python标准库中)不同,stdiomask
模块可以在您键入时显示***掩码字符。
用法示例:
>>> 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'
不幸的是,该模块像Python的内置getpass
模块一样,在IDLE或Jupyter Notebook中不起作用。
答案 8 :(得分:0)
首先安装这两个库
pip install getpass
pip install stdiomask
请注意,您安装的不是std io mask而是studiomask .....
然后是代码
password = stdiomask.getpass() # It will ask to enter password and display * on the screen
print(password)
这是解决方案 输出:
Password: *****
google
答案 9 :(得分:0)
我自己的建议是——不要这样做!
不要重新发明轮子,如果您想要“星星”,请使用密码助手
而是执行类似以下伪代码的操作...如果设置了 TTY_ASSPASS 环境变量 - 调用该密码助手。有很多可用的数字,包括“systemd-ask-password” 如果有 TTY - 回退读取没有回声 (getpass.getpass()) 如果没有 TTY, - 回退只读取 STDIN 与 rstrip()< /p>
这不仅让用户选择密码输入程序(通过环境变量(或其他一些配置),还让他们替代其他密码源,如 GUI 输入,或从预先打开的钥匙圈收集密码密码守护程序,或其他地方
不要限制或以其他方式限制密码的来源!
有关密码助手的说明,请参阅.. https://antofthy.gitlab.io/info/crypto/passwd_input.txt
对于 shell 脚本密码助手 https://antofthy.gitlab.io/software/#askpass_stars
看起来 Andrei Krivoshei 的回答已经开始了,结果非常相似,但仍处于起步阶段。