Python's getpass module exists for the sole purpose of getting login information from either the user or the environment. For security reasons, it accepts passwords without echoing the characters to the screen. It naturally lends itself to use with networking modules like poplib.
The getpass module does have some dependencies, however, if you use either Mac or Windows systems. To work on Windows systems, it requires the msvcrt module, a module from the Microsoft Visual C++ runtime environment. For Mac, it requires the availability of EasyDialogs (to call EasyDialogs.AskPassword). Unix users will need the termios module; sometimes this is disabled for security reasons. If these are not available when needed, it will throw an error and terminate.
The getpass module provides two basic methods:
- getpass([prompt]): Prompts the user for a password without echoing the keyboard input to the screen. It then returns the password as a raw string.
- getuser(): Gets the login name of the user. It first checks the environment variables ($LOGNAME, $USER, $LNAME, and $USERNAME). It then checks the system's password database. If it cannot find a username in any of these, it throws a KeyError. Note that this method is not available on Macs.