Python implements the Console Password Input Method, python Console
This example describes how to enter a password in the Python console. Share it with you for your reference. The details are as follows:
1. raw_input ():
pwd = raw_input('password: ')print pwd# password: aaa# aaa
Note: The simplest method, but not secure
2. getpass. getpass ():
import getpasspwd = getpass.getpass('password: ')print pwd# password:# aaaa
Note: It's safe, but you can't see the number of digits in the input. You may feel a little unaccustomed ..
3. msvcrt. getch ():
The Code is as follows:
Import msvcrt, sysdef pwd_input (): chars = [] while True: newChar = msvcrt. getch () if newChar in '\ r \ N': # if it is a line feed, enter the end print ''break elif newChar =' \ B ': # if it is a backspace, delete the last if chars: del chars [-1] sys. stdout. write ('\ B') # delete an asterisk, but do not know why it cannot be executed... else: chars. append (newChar) sys. stdout. write ('*') # print as an asterisk ''. join (chars) pwd = pwd_input () print pwd # ****** # aaaaaa
Note: The second method does not display the number of input digits. However, if you press the backspace key,
However, the console does not display the corresponding backspace. For example, if the current input is "abcd", it is displayed as "*****", and now a backspace key is entered.
The input value is abc, but it is still :****. I don't know why sys. stdout. write ('\ B') is not executed. It is estimated that it is related to msvcrt. getch. If you are interested, you can study it further.
I hope this article will help you with Python programming.