diff options
author | Guido van Rossum <guido@python.org> | 1998-04-09 20:37:16 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-04-09 20:37:16 (GMT) |
commit | b5903ac9fb67fd7f32aa223c5ed158ad6bb5293a (patch) | |
tree | 1cdf34c198f9bf4570225ce5d016c14c045ffc11 | |
parent | f2da4ed48b2cda073d6e5ea867eaa65559a69aef (diff) | |
download | cpython-b5903ac9fb67fd7f32aa223c5ed158ad6bb5293a.zip cpython-b5903ac9fb67fd7f32aa223c5ed158ad6bb5293a.tar.gz cpython-b5903ac9fb67fd7f32aa223c5ed158ad6bb5293a.tar.bz2 |
Another new utility: getpass() prompts for a password, with echo off.
Also contains getuser(), which returns the username (not prompting though).
These work on Unix and Windows!
-rw-r--r-- | Lib/getpass.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/Lib/getpass.py b/Lib/getpass.py new file mode 100644 index 0000000..44e78c2 --- /dev/null +++ b/Lib/getpass.py @@ -0,0 +1,79 @@ +"""Utilities to get a password and/or the current user name. + +getpass(prompt) - prompt for a password, with echo turned off +getuser() - get the user name from the environment or password database + +Authors: Piers Lauder (original) + Guido van Rossum (Windows support and cleanup) +""" + + +def getpass(prompt='Password: '): + """Prompt for a password, with echo turned off. + + Restore terminal settings at end. + + On Windows, this calls win_getpass(prompt) which uses the + msvcrt module to get the same effect. + + """ + + import sys + try: + import termios, TERMIOS + except ImportError: + return win_getpass(prompt) + + fd = sys.stdin.fileno() + old = termios.tcgetattr(fd) # a copy to save + new = old[:] + + new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags' + try: + termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new) + try: passwd = raw_input(prompt) + except KeyboardInterrupt: passwd = None + finally: + termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old) + + sys.stdout.write('\n') + return passwd + + +def win_getpass(prompt='Password: '): + """Prompt for password with echo off, using Windows getch().""" + import msvcrt + for c in prompt: + msvcrt.putch(c) + pw = "" + while 1: + c = msvcrt.getch() + if c == '\r' or c == '\n': + break + if c == '\b': + pw = pw[:-1] + else: + pw = pw + c + msvcrt.putch('\r') + msvcrt.putch('\n') + return pw + + +def getuser(): + """Get the username from the environment or password database. + + First try various environment variables, then the password + database. This works on Windows as long as USERNAME is set. + + """ + + import os + + for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): + user = os.environ.get(name) + if user: + return user + + # If this fails, the exception will "explain" why + import pwd + return pwd.getpwuid(os.getuid())[0] |