2

All in the title really...

I know you can use

getpass.getpass()

however, if distributing some code like this, people are used to having their characters by '*' so could give the impression that may make them believe their keyboard isn't working when they see no characters popping up on the screen.

I want to have 'example' show up as '*******' during the input, so that:

Enter Password: example

will show up as

Enter Password: *******

Thanks for any answers

1
  • You can change the prompt from getpass, e.g. getpass.getpass(prompt='Enter password (NB you receive no indication that a character was entered from e.g. an asterisk):') Commented May 4, 2017 at 8:18

3 Answers 3

2

I have a work around for you, which you can modify to your needs:

  1. Install getch: pip install getch. This module has methods for getting input char by char.
  2. Create a function that gets user input char by char and prints * in it's place:

    #import sys (if option 1. is used)
    import getch
    
    def get_password():
        p = ''
        typed = ''
    
        while True:
            typed = getch.getch()
    
            if typed == '\n':
                print(typed)
                break
    
            p += typed
    
            # Choose one of the following solutions:
    
            # 1. General way. Needs import sys
            sys.stdout.write('*')
            sys.stdout.flush()
    
            # 2. Python 3 way:
            print('*', end='', flush=True)
    
       return p
    

Good luck :)


EDIT: For @timgeb 's comment about security:

From getpass documentation:

On Unix, the prompt is written to the file-like object stream using the replace error handler if needed.
stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).

So it behaves quite similarly with the function above, except that mine has no fallback option...

Sign up to request clarification or add additional context in comments.

5 Comments

Can anybody judge the safety of this compared to using the getpass module?
I can't seem to install the getch module on Windows. What should I do?
In windows install msvcrt and use it as msvcrt.getch()
Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> get_password() File "C:\Users\Default\Desktop\kjhgh.py", line 15, in get_password p += typed TypeError: must be str, not bytes
See stackoverflow.com/questions/21689365/… also since you are using Python 3, you can use the print() option instead!
1

This one should do it:

import msvcrt
import sys

print('Enter your password: ')
password = ''
while True:
    pressedKey = msvcrt.getch()
    if pressedKey == '\r':    
       break
    else:
        password = password+pressedKey
        sys.stdout.write('*')

print('\n' + password)

It is MS VC++ - Microsoft Visual C++ that works for windows. getpass uses this module, according to docs

Comments

0

Really stop re-inventing the wheel. DO NOT include something like this in your program, but have it use a password helper program.

See https://stackoverflow.com/a/67327327/701532

Of course if you write a GREAT password helper for inputing stars in python, like I did many years ago in shell... Go for it!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.