python password

来源:互联网 发布:20网络用语什么意思 编辑:程序博客网 时间:2024/06/15 06:11
1. getpass.getpass() : 
import getpass 
pwd = getpass.getpass('password: ') 
print pwd 
# password: 
# aaaa 


2. msvcrt.getch() : 
代码 
import msvcrt, sys 

def pwd_input(): 
    chars = [] 
    while True: 
        newChar = msvcrt.getch() 
        if newChar in '\r\n': # 如果是换行,则输入结束 
            print '' 
            break 
        elif newChar == '\b': # 如果是退格,则删除末尾一位 
            if chars: 
                del chars[-1] 
                sys.stdout.write('\b') # 删除一个星号,但是不知道为什么不能执行... 
        else: 
            chars.append(newChar) 
            sys.stdout.write('*') # 显示为星号 
    print ''.join(chars) 

pwd = pwd_input() 
print pwd 
原创粉丝点击