http://docs.python.org/2/library/getpass.html
The getpass module provides two functions:
-
getpass.
getpass
(
[
prompt
[,
stream
]
]
)
-
Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '.
Many programs that interact with the user via the terminal need to ask the user for password values without showing what the user types on the screen. The getpass module provides a portable way to handle such password prompts securely.
Example
The getpass() function prints a prompt then reads input from the user until they press return. The input is passed back as a string to the caller.
import getpass
p = getpass.getpass()
print 'You entered:', p
The default prompt, if none is specified by the caller, is “Password:”.
$ python getpass_defaults.py
Password:
You entered: sekret
The prompt can be changed to any value your program needs.
import getpass
p = getpass.getpass(prompt='What is your favorite color? ')
if p.lower() == 'blue':
print 'Right. Off you go.'
else:
print 'Auuuuugh!'
I don’t recommend such an insecure authentication scheme, but it illustrates the point.
$ python getpass_prompt.py
What is your favorite color?
Right. Off you go.
$ python getpass_prompt.py
What is your favorite color?
Auuuuugh!