Coders Packet

Getpass Module in Python

By Abhishek Kumar

Getpass module has two main functions one is to find the existing user and the other is to hide the password from the terminal.

 

Why we use the getpass Module  : 

     --> Getpass module helps us in two ways one way is it helps us to hide the password which we type in the

           terminal. And it also helps us to find the existing working user, in which user we are currently working.

 

          How to install the getpass module in Windows :

 

            pip install getpass

 

          How to import getpass module :

 

           import getpass

 

    -->  Getpass Module has basically two functions : 

             1. getpass()

             2. getuser()

 

What getpass() function offers  :

 

     --> getpass() prompts the user for a password without displaying it in the terminal.

     --> The getpass module provides a secure way to handle the password prompts where programs interact

            with a user via the terminal.

 

           Let's understand this getpass() function using two examples.

 

Example 1. Without using getpass() function.                        

# Just imagine we have to login in a server. 
# We know the password and username of the server.

username="abhishek" password="12345"
-----------------------------------------------------------------------------------------------------------------------------
# Internal programming in the server for login.

# We create a variable Which take a string value as a Username. usname=input("Enter a username : ")
# strip() Function is used to remove the extra space present in the begining and
# end of the string. usname=usname.strip()

# We check if the entered username is matching to the existing user. if usname!=username:
# If not matched then We exit from the server. print("Incorrect user name . Bye") else:
# If matched Then we ask them to enter password. pword=input("Enter your password : ")

# Then we check whether the entered password is matching with the existing username password. if pword == password:

# If matched then we entered in the server. print("Welcome to the Coderspacket.com") else:

# If not then we simply close the server. print("Incorrect Password.")

 OUTPUT :  Enter a username : Abhishek

                  Enter  your password : 12345

                  Welcome to the coderspacket.com

                        

Example 2. Using getpass() function.

# Just imagine we have to login in a server. 
# We know the password and username of the server.
username="abhishek" password="12345"
-----------------------------------------------------------------------------------------------------------------------------
# Internal programming in the server for login.


# In this example we want to hide the password in the terminal so
# we import the getpass module. from getpass import getpass

# We create a variable Which take a string value as a Username. usname=input("Enter a username : ")

# strip() Function is used to remove the extra space present in the begining and
# end of the string. usname=usname.strip()

# We check if the entered username is matching to the existing user. if usname!=username:

# If not matched then We exit from the server. print("Incorrect user name . Bye") else:

# If matched Then we ask them to enter password.
# We are using here one of the function of the getpass module which take pasword as input. pword=getpass("Enter your password : ")

# Then we check whether the entered password is matching with the existing username password. if pword == password:

# If matched then we entered in the server. print("Welcome to the Coderspacket.com") else:

# If not then we simply close the server. print("Incorrect Password.")

OUTPUT :  Enter a username : Abhishek

                  Enter  your password : *****

                  Welcome to the coderspacket.com

 

 

What we basically learn from these examples. 

  --> When we do not use the getpass function then the password is visible in the terminal which is not good 

        for any user and it will affect the privacy of a user.

  -->  When we use the getpass() function then the password is not visible in the terminal which secures the

         privacy of the user.

  -->  So therefore we use the getpass() function.

                            

What getuser() function offers  :

 

   --> The getuser() function display the login name of the user. Sometimes we need to know the login name

         which we are using to run the script. This is achieved by the getuser() function.

         Let understand getuser() function with one example : 

           

Example 1 :

# Just imagine we have to login in a server. 
# We know olny the password not the username of the server.
password="abcd"
----------------------------------------------------------------------------------------------------------------------------
# Internal programming in the server for login.

# We Import the getpass module so that we can use the function getuser()
# Which help to find the username of the existing username. import getpass


# We have used getuser Function which returns the existing username. user = getpass.getuser()
while True :

# pwd Veriable takes the password of the existing username. pwd = getpass.getpass(""" User Name : %s Password : """ % user)

# Then we check whether the entered password is matching with the existing username password. if pwd == password:

# If matched then we entered in the server. print (" Welcome!!!") break else:

# If not then we simply close the server. print (" The password you entered is incorrect.")

OUTPUT :     User Name : Kumar
                    Password : *****
                    Welcome!!!

 

What we basically learn from these examples. 

    --> If we are working in a system and we do not know the user name of the system,

          Then getuser() function helps us to know the username as it returns the existing username.

 

Conclusion :

    --> getuser() help us to know the existing or current username of the working system.

    -->  getpass() helps to hide the password from the terminal.

 

Requirement :

    -->  Python must be installed in your working system.

    -->  Jupiter notebook is required.

 

 

Download project

Reviews Report

Submitted by Abhishek Kumar (kumarabhishek03)

Download packets of source code on Coders Packet