In this post we will be creating a Python script that will verify a set of user credentials, to help us achieve this we will be using the ‘getpass’ module. The “getpass” module in Python provides a secure way to collect sensitive information, such as passwords, from the user.
See the snippet of Python code below where we utilize the ‘getpass’ module to verify some user credentials. Below we make use of the ‘getpass()’ function to conceal what is typed as a password. Using the ‘prompt’ argument we are able to set the text displayed to the user. Once the credentials have been entered we then check if they match our defined credentials where the relevant access denied or granted message is displayed.
For the purpose of this example the username has been set to ‘scriptopia’. And the password has been set to ‘password’.
import getpass
import time
username = input("Enter your username: ")
password = getpass.getpass(prompt="Enter your password: ")
if username == "scriptopia" and password == "password":
print("Access granted.")
time.sleep(5)
else:
print("Access denied.")
time.sleep(5)
Take a look at some of our other content around the Python programming language by clicking here.