import msvcrt, sys, os
print('password: ', end='', flush=True)
li = []
while 1:
ch = msvcrt.getch()
#回车
if ch == b'\r':
msvcrt.putch(b'\n')
print('输入的密码是:%s' % b''.join(li).decode())
break
#退格
elif ch == b'\x08':
if li:
li.pop()
msvcrt.putch(b'\b')
msvcrt.putch(b' ')
msvcrt.putch(b'\b')
#Esc
elif ch == b'\x1b':
break
else:
li.append(ch)
msvcrt.putch(b'*')
os.system('pause')
示例
一、raw_input()或input():
for python 2.x
[root@master test]# /usr/local/python2.7/bin/python test.py
Please input your password:123
your password is 123
[root@master test]# cat test.py
#!/usr/bin/python
# -*- coding=utf-8 -*-
#for python 2.x
#input = raw_input("Please input your password:")
#print "your password is %s" %input
for python 3.x
[root@master test]# /usr/local/python3.4/bin/python3 test.py
Please input your password:123
your password is 123
[root@master test]# cat test.py
#!/usr/bin/python
# -*- coding=utf-8 -*-
#for python 3.x
input = input("Please input your password:")
print ("your password is %s" %input)
Note:这种方法最简单,但是不安全,很容易暴露密码。
二、getpass.getpass():
for python 2.x
[root@master test]# /usr/local/python2.7/bin/python test.py
Please input your password:
your password is 123
[root@master tes