How do I convert a password into asterisks while it is being entered?

前端 未结 9 1980
感情败类
感情败类 2020-11-28 13:31

Is there a way in Python to convert characters as they are being entered by the user to asterisks, like it can be seen on many websites?

For example, if an email use

9条回答
  •  死守一世寂寞
    2020-11-28 14:07

    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'
    

    Unfortunately this module, like Python's built-in getpass module, doesn't work in IDLE or Jupyter Notebook.

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

提交回复
热议问题