• Python:控制台输入密码passwod的方法


    input

    print(input("please input: "))
    
    • 1
    $ python3 demo.py 
    please input: 123456
    123456
    
    • 1
    • 2
    • 3

    缺点:不安全

    getpass

    import getpass
    
    print(getpass.getpass("please input: "))
    
    • 1
    • 2
    • 3
    $ python3 demo.py 
    please input: 
    123456
    
    • 1
    • 2
    • 3

    缺点:看不到输入的位数

    termios

    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
    
    def getpass(maskchar = "*"): 
      password = "" 
      while True: 
        ch = getch() 
        if ch == "\r" or ch == "\n": 
          print 
          return password 
        elif ch == "\b" or ord(ch) == 127: 
          if len(password) > 0: 
            sys.stdout.write("\b \b") 
            password = password[:-1] 
        else: 
          if maskchar != None: 
            sys.stdout.write(maskchar) 
          password += ch 
    
    if __name__ == "__main__": 
      print ("Enter your password:",)
      password = getpass("*") 
      print ("your password is %s" %password)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    $ python3 demo.py 
    Enter your password:
    ******
    your password is 123456
    
    • 1
    • 2
    • 3
    • 4

    缺点:该方法仅在Linux上使用

    msvcrt

    import msvcrt,sys
    
    def pwd_input():    
        chars = []   
        while True:  
            try:  
                newChar = msvcrt.getch().decode(encoding="utf-8")  
            except: 
                return input("你很可能不是在cmd命令行下运行,密码输入将不能隐藏:")  
            if newChar in '\r\n': # 如果是换行,则输入结束               
                 break   
            elif newChar == '\b': # 如果是退格,则删除密码末尾一位并且删除一个星号   
                 if chars:    
                     del chars[-1]   
                     msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格  
                     msvcrt.putch( ' '.encode(encoding='utf-8')) # 输出一个空格覆盖原来的星号  
                     msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格准备接受新的输入                   
            else:  
                chars.append(newChar)  
                msvcrt.putch('*'.encode(encoding='utf-8')) # 显示为星号  
        return (''.join(chars) )  
      
    if __name__ == "__main__": 
        print("Please input your password:")
        pwd = pwd_input()  
        print("\nyour password is:{0}".format(pwd))
        sys.exit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    缺点:仅在Windows上使用

    参考

  • 相关阅读:
    Java.lang.Class类 getResourceAsStream()方法有什么功能呢?
    金三银四好像消失了,IT行业何时复苏!
    24、D-NeRF: Neural Radiance Fields for Dynamic Scenes
    ​UWA报告使用技巧小视频,你get了么?(第六弹)
    Tomcat深入浅出(一)
    Python装饰器
    Android log 系统
    算法设计与分析 SCAU17089 最大m子段和
    用Python做个学生管理系统,这不简简单单
    广州蓝景分享—前端开发JavaScript中的Array对象与其他数组
  • 原文地址:https://blog.csdn.net/mouday/article/details/134065397