Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 1 | """Utilities to get a password and/or the current user name. |
| 2 | |
| 3 | getpass(prompt) - prompt for a password, with echo turned off |
| 4 | getuser() - get the user name from the environment or password database |
| 5 | |
| 6 | Authors: Piers Lauder (original) |
| 7 | Guido van Rossum (Windows support and cleanup) |
| 8 | """ |
| 9 | |
| 10 | |
| 11 | def getpass(prompt='Password: '): |
| 12 | """Prompt for a password, with echo turned off. |
| 13 | |
| 14 | Restore terminal settings at end. |
| 15 | |
| 16 | On Windows, this calls win_getpass(prompt) which uses the |
| 17 | msvcrt module to get the same effect. |
| 18 | |
| 19 | """ |
| 20 | |
| 21 | import sys |
| 22 | try: |
| 23 | import termios, TERMIOS |
| 24 | except ImportError: |
Guido van Rossum | fb9b7fd | 1998-04-13 20:22:21 +0000 | [diff] [blame] | 25 | try: |
| 26 | import msvcrt |
| 27 | except ImportError: |
| 28 | return default_getpass(prompt) |
| 29 | else: |
| 30 | return win_getpass(prompt) |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 31 | |
| 32 | fd = sys.stdin.fileno() |
| 33 | old = termios.tcgetattr(fd) # a copy to save |
| 34 | new = old[:] |
| 35 | |
| 36 | new[3] = new[3] & ~TERMIOS.ECHO # 3 == 'lflags' |
| 37 | try: |
| 38 | termios.tcsetattr(fd, TERMIOS.TCSADRAIN, new) |
Guido van Rossum | c3da02e | 1998-06-12 14:28:38 +0000 | [diff] [blame] | 39 | passwd = raw_input(prompt) |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 40 | finally: |
| 41 | termios.tcsetattr(fd, TERMIOS.TCSADRAIN, old) |
| 42 | |
| 43 | sys.stdout.write('\n') |
| 44 | return passwd |
| 45 | |
| 46 | |
| 47 | def win_getpass(prompt='Password: '): |
| 48 | """Prompt for password with echo off, using Windows getch().""" |
| 49 | import msvcrt |
| 50 | for c in prompt: |
| 51 | msvcrt.putch(c) |
| 52 | pw = "" |
| 53 | while 1: |
| 54 | c = msvcrt.getch() |
| 55 | if c == '\r' or c == '\n': |
| 56 | break |
Guido van Rossum | c3da02e | 1998-06-12 14:28:38 +0000 | [diff] [blame] | 57 | if c == '\003': |
| 58 | raise KeyboardInterrupt |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 59 | if c == '\b': |
| 60 | pw = pw[:-1] |
| 61 | else: |
| 62 | pw = pw + c |
| 63 | msvcrt.putch('\r') |
| 64 | msvcrt.putch('\n') |
| 65 | return pw |
| 66 | |
| 67 | |
Guido van Rossum | fb9b7fd | 1998-04-13 20:22:21 +0000 | [diff] [blame] | 68 | def default_getpass(prompt='Password: '): |
| 69 | return raw_input(prompt) |
| 70 | |
| 71 | |
Guido van Rossum | b5903ac | 1998-04-09 20:37:16 +0000 | [diff] [blame] | 72 | def getuser(): |
| 73 | """Get the username from the environment or password database. |
| 74 | |
| 75 | First try various environment variables, then the password |
| 76 | database. This works on Windows as long as USERNAME is set. |
| 77 | |
| 78 | """ |
| 79 | |
| 80 | import os |
| 81 | |
| 82 | for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): |
| 83 | user = os.environ.get(name) |
| 84 | if user: |
| 85 | return user |
| 86 | |
| 87 | # If this fails, the exception will "explain" why |
| 88 | import pwd |
| 89 | return pwd.getpwuid(os.getuid())[0] |