Reputation: 11346
You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?
Is there a way to do that in Python? I'm working on a script that requires so sensitive info and would like for it to be hidden when I'm typing it.
In other words, I want to get the password from the user without showing the password.
Upvotes: 426
Views: 448147
Reputation: 76
I recommend using pyautogui. Pyautogui is a great GUI automation module. It can access the mouse, keyboard, find images on screen (uses Pillow), and also display alert boxes, confirmation boxes, and buttons. In this case, the function that I suggest is password(). For example:
import pyautogui
password = pyautogui.password('Enter password: ') #password hidden with *
print(password)
If you want to learn more about how pyautogui can be used for buttons, confirmation boxes, and alert boxes, or for any other things, you can check out the information here. Full documentation.
Upvotes: 1
Reputation: 73
You can also use the pwinput module which works on both Windows and Linux. It replaces the char with '*' (by default) and backspace works.
import pwinput
password = pwinput.pwinput(prompt='Password: ')
You can, optionally, pass a different mask
character as well.
import pwinput
password = pwinput.pwinput(prompt='Password: ', mask='')
See the pwinput documentation for more information.
Upvotes: 5
Reputation: 601341
Use getpass.getpass()
:
from getpass import getpass
password = getpass()
An optional prompt can be passed as parameter; the default is "Password: "
.
Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.
Upvotes: 589
Reputation: 404
Here is my code based off the code offered by @Ahmed ALaa
Features:
*
character (DEC: 42 ; HEX: 0x2A)
instead of the input characterDemerits:
The function secure_password_input()
returns the password as a string
when called. It accepts a Password Prompt string, which will be displayed to the user to type the password
def secure_password_input(prompt=''):
p_s = ''
proxy_string = [' '] * 64
while True:
sys.stdout.write('\x0D' + prompt + ''.join(proxy_string))
c = msvcrt.getch()
if c == b'\r':
break
elif c == b'\x08':
p_s = p_s[:-1]
proxy_string[len(p_s)] = " "
else:
proxy_string[len(p_s)] = "*"
p_s += c.decode()
sys.stdout.write('\n')
return p_s
Upvotes: 1
Reputation: 179
Updating on the answer of @Ahmed ALaa
# import msvcrt
import getch
def getPass():
passwor = ''
while True:
x = getch.getch()
# x = msvcrt.getch().decode("utf-8")
if x == '\r' or x == '\n':
break
print('*', end='', flush=True)
passwor +=x
return passwor
print("\nout=", getPass())
msvcrt us only for windows, but getch from PyPI should work for both (I only tested with linux). You can also comment/uncomment the two lines to make it work for windows.
Upvotes: 4
Reputation: 257
This code will print an asterisk instead of every letter.
import sys
import msvcrt
passwor = ''
while True:
x = msvcrt.getch()
if x == '\r':
break
sys.stdout.write('*')
passwor +=x
print '\n'+passwor
Upvotes: 24