Masking user input in python with asterisks

℡╲_俬逩灬. 提交于 2019-11-26 05:38:48

问题


I am trying to mask what the user types into IDLE with asterisks so people around them can\'t see what they\'re typing/have typed in. I\'m using basic raw input to collect what they type.

key = raw_input(\'Password :: \')

Ideal IDLE prompt after user types password:

Password :: **********

回答1:


Depending on the OS, how you get a single character from user input and how to check for the carriage return will be different.

See this post: Python read a single character from the user

On OSX, for example, you could so something like this:

import sys, tty, termios

def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

key = ""
sys.stdout.write('Password :: ')
while True:
    ch = getch()
    if ch == '\r':
        break
    key += ch
    sys.stdout.write('*')
print
print key



回答2:


If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the stdiomask module:

pip install stdiomask

Unlike getpass.getpass() (which is in the Python Standard Library), the stdiomask module can display *** mask characters as you type.

Example usage:

>>> stdiomask.getpass()
Password: *********
'swordfish'
>>> stdiomask.getpass(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> stdiomask.getpass(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> stdiomask.getpass(mask='') # Don't display anything.
Password:
'swordfish'

More details at https://pypi.org/project/stdiomask/




回答3:


To solve this I wrote this small module pyssword to mask the user input password at the prompt. It works with windows. The code is below:

from msvcrt import getch
import getpass, sys

def pyssword(prompt='Password: '):
    '''
        Prompt for a password and masks the input.
        Returns:
            the value entered by the user.
    '''

    if sys.stdin is not sys.__stdin__:
        pwd = getpass.getpass(prompt)
        return pwd
    else:
        pwd = ""        
        sys.stdout.write(prompt)
        sys.stdout.flush()        
        while True:
            key = ord(getch())
            if key == 13: #Return Key
                sys.stdout.write('\n')
                return pwd
                break
            if key == 8: #Backspace key
                if len(pwd) > 0:
                    # Erases previous character.
                    sys.stdout.write('\b' + ' ' + '\b')                
                    sys.stdout.flush()
                    pwd = pwd[:-1]                    
            else:
                # Masks user input.
                char = chr(key)
                sys.stdout.write('*')
                sys.stdout.flush()                
                pwd = pwd + char



回答4:


Disclaimer: does not provide the asterix in terminal, but it does so in jupyter notebook.

The below code provides replaces written characters with asterix and allow for deletion of wrongly typed characters. The number of asterixes reflects the number of typed characters.

import getpass
key = getpass.getpass('Password :: ')

And after the user press enter:



来源:https://stackoverflow.com/questions/27631629/masking-user-input-in-python-with-asterisks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!