Home >Backend Development >Python Tutorial >How Can I Securely Obtain Concealed Password Input in Python?
Obtaining Concealed Password Input in Python
Securing sensitive information is paramount in scripting. One prevalent technique used in Linux is concealing password input during entry, as seen when executing Sudo commands. This ensures the privacy of passwords during authentication.
Transposing this functionality into Python scripts can be achieved through the getpass.getpass() function. Its simplicity and effectiveness make it an ideal choice for discreet password acquisition. Here's how you can leverage it:
from getpass import getpass password = getpass.getpass()
This code snippet prompts the user to enter a password without displaying the characters typed. Instead, the terminal shows asterisks or other non-revealing characters.
Optionally, you can provide a custom prompt as an argument to getpass.getpass(). By default, it uses "Password: " as the prompt.
Remember that this function operates based on the availability of a terminal that supports echo control. When executed within IDLE, it may fail with an echoing issue. For more information, refer to the documentation related to "GetPassWarning: Can not control echo on the terminal."
The above is the detailed content of How Can I Securely Obtain Concealed Password Input in Python?. For more information, please follow other related articles on the PHP Chinese website!