Password prompt with mask
Neuberger, Sheldon N.
sneuberger at mitre.org
Tue Aug 5 14:29:36 EDT 2008
> Could I write my own if one does not exist?
Take a look at the getpass.py module. It's very short. The windows
version of the getpass function can be trivially modified to echo
something. The unix version is just as short but a bit more
complicated.
def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
if sys.stdin is not sys.__stdin__:
return default_getpass(prompt, stream)
import msvcrt
for c in prompt:
msvcrt.putch(c)
pw = ""
while 1:
c = msvcrt.getch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw
More information about the Python-list
mailing list