1. raw_input() :

pwd = raw_input('password: ')
print pwd
# password: aaa
#
 aaa


Note: 最简单的方法,但是不安全

 

2. getpass.getpass() :

import getpass
pwd = getpass.getpass('password: ')
print pwd
# password:
# aaaa


Note: 很安全,但是看不到输入的位数,会让人觉得有点不习惯,不知道的还以为没有在输入..

 

3. 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

# ******
#
 aaaaaa

相关文章: