Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Masking user input in python with asterisks

I am trying to mask what the user types into IDLE with asterisks so people around them can't see what they're typing/have typed in. I'm using basic raw input to collect what they type.

key = raw_input('Password :: ')

Ideal IDLE prompt after user types password:

Password :: **********
like image 679
Jackson Blankenship Avatar asked Dec 24 '14 04:12

Jackson Blankenship


1 Answers

If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput module:

pip install pwinput

Unlike getpass.getpass() (which is in the Python Standard Library), the pwinput module can display *** mask characters as you type.

Example usage:

>>> pwinput.pwinput()
Password: *********
'swordfish'
>>> pwinput.pwinput(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> pwinput.pwinput(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> pwinput.pwinput(mask='') # Don't display anything.
Password:
'swordfish'

Unfortunately this module, like Python's built-in getpass module, doesn't work in IDLE or Jupyter Notebook.

More details at https://pypi.org/project/pwinput/

Note that pwinput is the new name for the stdiomask module.

like image 117
Al Sweigart Avatar answered Oct 05 '22 10:10

Al Sweigart