Home >Backend Development >Python Tutorial >A detailed introduction to the getpass module in Pythong
I recently stumbled upon this module while reading the official documentation of the Python standard library. After a closer look, the content is quite small. There are only two main APIs. I spent some time reading the source code and found it to be quite practical. I would like to share it with you here. The following article mainly introduces you to the relevant information about the basic getpass module of Python. Friends in need can refer to it.
This article mainly introduces to you the relevant content about the getpass module in Python, and shares it for your reference and study. Without further ado, let’s take a look at the detailed introduction:
getpass module Provides a platform-independent method of entering passwords on the command line;
This module mainly provides:
Two functions: getuser, getpass
An alarm: GetPassWarning (thrown when the entered password may be displayed. This alarm is a subclass of UserWarning)
Remarks: The above is the alarm thrown when the password is displayed
getpass.getuser()
This function returns the logged in user name. No parameters are required
This function checks the environment variables LOGNAME, USER, LNAME and USERNAME to return a non-empty string. If these variables are set to empty, the username will be obtained from a database that supports passwords, otherwise an exception that the user cannot be found will be triggered!
getpass.getpass([prompt[, stream]])
will display the prompt string, turn off the screen echo of the keyboard, and then read the password
can bring the prompt, Without a prompt, the default prompt 'Password: '
' will be entered. In Linux/Unix systems, the prompt will be written to the class file stream and written to /dev/tty by default. If it cannot be written, it will be written to sys.stderr
. If the password is displayed when calling the getpass()
function, a GetPassWarning alarm will be thrown, which is read from sys.stdin
Remarks: When the getpass function is adjusted in IDLE, the entered password will be displayed. The password must be displayed in Python Shell or CMD under Windows
[ Example】
Result output:-->
# #Summarize
The above is the detailed content of A detailed introduction to the getpass module in Pythong. For more information, please follow other related articles on the PHP Chinese website!