Hide password on terminal

Asked

Viewed 1,757 times

5

I am developing a python application that will work by the terminal

At a certain point, I need to login:

print('É necessário informar suas credenciais para se comunicar com a API')
email = input('Email: ')
password = input('Senha: ')

The problem is that the output on the console stays that way:

É necessário informar suas credenciais para se comunicar com a API:
Email: [email protected]
Senha: senhaTeste

I need the password field to not show the characters, replace them with "*" or something like that:

The exit I look for in the terminal is something like this:

É necessário informar suas credenciais para se comunicar com a API:
Email: [email protected]
Senha: **********

2 answers

5


You can use the getpass.getpass

import getpass

print('É necessário informar suas credenciais para se comunicar com a API')
email = input('Email: ')
password = getpass.getpass('Senha: ')
  • Great, that already solves my problem, but do you know if there is a possibility instead of being blank with each letter insert a '*'? For those who are using can realize that they are actually typing

  • 1

    Then just implementing something, @Joséhenriqueluckmann. I don’t think it’s a good idea, I’ve never seen a terminal show asterisks while typing a password, and it’s much more work for little result.

  • 2

    Do not appear * on terminals for safety... Knowing the amount of characters a password is much easier to make a Joy force... It’s a design decision

  • And +1 for the answer and the question. I didn’t know this module

1

If you want it to happen in the terminal, you will have to create a new input function and import it after, from the original input() function in C ( available here from line 1832 ). It seems to be relatively easy if you understand a little bit how the compiler works. As far as I know, there is still no high-level solution to this problem.

  • Very good to know, for the moment the solution presented by @LINQ is the most viable. But when I have some time I’ll play a little with it and try to develop

Browser other questions tagged

You are not signed in. Login or sign up in order to post.