Python getpass module

28/12/2020
We all keep secrets, don’t we? Even our programs do. The important thing about secrets is that we must keep them safely and secretly hidden from entities who should not have access to these secret keys, pass-phrases or password which will allow users to do something they aren’t authorized to do. Same care needs to be taken when we accept secret keys, pass-phrases or password from users in our programs like the secret String should not be echoed when we type them on the screen. The Python getpass module allows us to do this efficiently.

Python getpass module

Using Python getpass module, it is possible to accept passwords in Python programs and keep the passphrases safe. We will see examples of keeping passwords safe and also how we can stream passwords from the terminal to text files. Let’s get started with some examples.

Python getpass basic example

In this example we will start with a very basic example of how we can make a user enter a password in the terminal and make sure that the password is not echoed back to the command prompt. Here is the same program:

import getpass

try:
password = getpass.getpass()
except Exception as ex:
print(‘Error Occured : ‘, ex)
else:
print(‘Entered password :’, password)

Here is what we get back with this command:

Get password secret

The string password is the default prompt which is presented by the python script. In the next example will be customising that to something we will like to use in our programs.

Python getpass with custom prompt

In this example we will customise the prompt which is shown to the user when Python asks for a secret phrase:

import getpass

pwd = getpass.getpass(prompt = ‘Which is best Ubuntu island to visit?’)
if pwd == ‘LinuxHint’:
print(‘Ofcourse!’)
else:
print(‘Where is that?’)

Let’s see the output for this command:

Custom prompt to get Password

This command is useful when you want to ask for some passphrases apart from password strings.

Stream password to another streaml

The getpass module allows us to stream the password a user enters to some other streams like a file, logs or anything which can be represented as a stream actually. We just need to pass the stream to the function itself:

import getpass
import sys

pwd = getpass.getpass(stream=sys.stderr)
print(‘Entered Password: ‘, pwd)

Here is what we get back with this command:

Streaming password to other streams

Getting passwords without Terminal

The Python getpass module needs tty which can be controlled by a termios. This is applicable when we are working with some Unix based systems. With this, echoing can be disabled. When we execute the following example on a non-Unix machine:

echo "not dark" | python3 getpass_defaults.py

Here is what we get back with this command:

termios command

As we ran the script on a non-Unix machine, the output String was what we entered. Otherwise, we would have simply seen not dark String as output on the terminal.

Read more about teletype terminals here.

Conclusion

In this lesson, we looked at how we can make use of Python getpass module to manage secret passphrases efficiently in our Python programs.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Parsing HTML using Python

Parsing HTML is one of the most common task done today to collect information from the websites and mine it for various...
28/12/2020

shutil module in Python

File Management and Handling file objects are considered to be one of the most tricky tasks in all programming languages....
28/12/2020

Python QR Code generator

What are QR codes? QR codes are some of the fastest-growing trends in today’s world when it comes to sharing any sort...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn fake ip bằng phần mềm SStap
10/06/2025

VPS treo game là gì? Thuê VPS treo game giá rẻ, không lo giật lag
02/06/2025

 BitBrowser – Best Anti-Detect Browser!
26/05/2025

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025