python pass2.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pass2.py相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

def main():
    print("password is: " + get_passwd())

def get_passwd():
    """
    getpass alternative that prints asterisks after each character.
    only tested with utf-8 locales.
    """
    import termios
    import sys
    import tty
    import locale

    # get locale for decoding input
    locale.setlocale(locale.LC_ALL, '')
    loc, encoding = locale.getlocale()

    # prepare terminal states
    fd = sys.stdin.fileno()
    term_state = termios.tcgetattr(fd)
    new_state = termios.tcgetattr(fd)
    new_state[3] = new_state[3] & ~termios.ECHO

    passwd = b""
    ctrl_c = False
    try:
        # disable echo
        termios.tcsetattr(fd, termios.TCSADRAIN, new_state)
        tty.setraw(fd)
        done = False
        while not done:
            try:
                # Python 3
                c = sys.stdin.buffer.read(1)
            except AttributeError:
                # Python 2
                c = sys.stdin.read(1)
            # \n or \r, end of password
            if ord(c) == 10 or ord(c) == 13:
                done = True
            # ctrl+c
            elif ord(c) == 3:
                done = True
                ctrl_c = True
            # backspace
            elif ord(c) == 127:
                passwd = passwd[:-1]
                sys.stdout.write("\b \b")
                sys.stdout.flush()
            # default: append
            else:               
                passwd += c
                try:
                    # if this fails, we don't have a full (utf-8) character
                    passwd.decode(encoding)
                    sys.stdout.write("*")
                    sys.stdout.flush()
                except UnicodeDecodeError:
                    pass
    
    finally:
        sys.stdout.write("\r\n")
        sys.stdout.flush()
        termios.tcsetattr(fd, termios.TCSADRAIN, term_state)
        pass
    if ctrl_c:
        sys.exit(0)
    return passwd.decode(encoding)


if __name__ == "__main__":
    main()

以上是关于python pass2.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python