展开全部
其实最简单的方法是import getpass 这个里面有方法可以不回显的32313133353236313431303231363533e59b9ee7ad9431333335333736输入密码,但是想用*回显的话就比较麻烦了。。可以用下面这个方法。调用getpass方法,参数默认使用`*`回显,可以修改成其他回显字符。。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":
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 password:",
password = getpass("*")
print password