Python Library Reference
Previous: Up: 16.7 termios Next:

16.7.1 Example

Here's a function that prompts for a password with echoing turned off. Note the technique using a separate tcgetattr() call and a try ... finally statement to ensure that the old tty attributes are restored exactly no matter what happens:

def getpass(prompt = "Password: "):
 import termios, sys
 fd = sys.stdin.fileno()
 old = termios.tcgetattr(fd)
 new = termios.tcgetattr(fd)
 new[3] = new[3] & ~termios.ECHO # lflags
 try:
 termios.tcsetattr(fd, termios.TCSADRAIN, new)
 passwd = raw_input(prompt)
 finally:
 termios.tcsetattr(fd, termios.TCSADRAIN, old)
 return passwd

Python Library Reference
Previous: Up: 16.7 termios Next:

Release 2.5, documentation updated on 19th September, 2006.
See About this document... for information on suggesting changes.

AltStyle によって変換されたページ (->オリジナル) /