又战斗到天明了。
成果:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import termios, sys, os
TERMIOS = termios
def getch():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c
def passwd():
chars = ''
while True:
newChar = getch()
if newChar in '=\r\n': # 如果是换行,则输入结束
break
elif newChar == '': # 如果是退格,则删除密码末尾一位并且删除一个星号
if chars:
chars = chars[:-1]
sys.stdout.write('\b \b')
sys.stdout.flush()
else:
# chars.append(newChar)
chars += newChar
sys.stdout.write('*')
sys.stdout.flush()
return ('\n'+chars)
'''
password=''
while 1:
ch = getch()
if ch == '\b':
if password:
password = password[:-1]
sys.stdout.write('\b \b')
sys.stdout.flush()
else:
continue
elif ch == '\n':
sys.stdout.write(os.linesep)
return password
else:
password += ch
sys.stdout.write('*')
sys.stdout.flush()
'''
print passwd()
没有评论:
发表评论