Asking for passwords
In terminal-based programs, it's common to ask for passwords from our users. It's usually a bad idea to do so from command options, as on Unix-like systems, they will be visible to anyone with access to the shell who is able to run a ps
command to get the list of processes, and to anyone willing to run a history
command to get the list of recently executed commands.
While there are ways to tweak the command arguments to hide them from the list of processes, it's always best to ask for passwords interactively so that no trace of them is left.
But, asking for them interactively is not enough, unless you also ensure they are not displayed while typing, otherwise anyone looking at your screen can grab all your passwords.
How to do it...
Luckily, the Python standard library provides an easy way to input passwords from a prompt without showing them back:
>>> import getpass
>>> pwd = getpass.getpass()
Password:
>>> print(pwd)
'HelloWorld'