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

Top 10 Python IDE for Ubuntu

Python is one of the most widely used general purpose programming languages. Many of the popular websites or application...
28/12/2020

Python gzip module

In this lesson, we will study about how we can make use of Python gzip module to read & writing into the compressed...
28/12/2020

Use python to zip a file and directory

A compressed file contains many files, directory and subdirectories. Many applications are available to create a compress...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024