1.

What is the purpose of getpass module in standard library?

Answer»

We normally come across LOGIN page of web application where a secret input such as a password or PIN is entered in a text field that masks the actual keys. How do we accept user input in a console based program? This is where the getpass module proves useful.

The getpass module provides a portable way to handle such password inputs securely so that the program can interact with the user via the terminal without showing what the user TYPES on the screen.

The module has the getpass() function which prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '.

import getpass pwd=getpass.getpass() print ('you entered :', pwd)

Run above script as example.py from command line:

$ PYTHON example.py Password: you entered: test

You can change the default prompt by explicitly defining it as argument to getpass() function

pwd=getpass.getpass(prompt='enter your password: ') $ python example.py enter your password: you entered test

The getpass() function can also receive optional stream argument to redirect the prompt to a file LIKE object such as sys.stderr – the default value of stream is sys.stdout.

There is also a getuser() function in this module. It returns the “login name” of the user by CHECKING the environment variables LOGNAME, USER, LNAME and USERNAME.



Discussion

No Comment Found