1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
"""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:
try:
import msvcrt
except ImportError:
return default_getpass(prompt)
else:
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)
passwd = raw_input(prompt)
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 == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw
def default_getpass(prompt='Password: '):
return raw_input(prompt)
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]
|