I am trying to figure out how to suppress the display of user input on stdout.
raw_input()
followed by any print statement preserves what the user typed
The following code, based on the Python docs, uses the termios
module and seems to do what you want (although it is not as compact as VT100 control codes):
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
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
passwd = raw_input(prompt)
print '\r \r',
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return passwd
p = getpass()
There are two tricky lines: one disables the echo, the other one erases your password prompt remaining at the first position of the line.