On 17/08/11 10:03:00, peter wrote:
Is there an equivalent to msvcrt for Linux users? I haven't found one, and have resorted to some very clumsy code which turns off keyboard excho then reads stdin. Seems such an obvious thing to want to do I am surprised there is not a standard library module for it. Or have I missed someting (wouldn't be the first time!)
The quick and dirty way is to invoke stty(1) using os.system: import os def getpassword(prompt="Password: "): try: os.system("stty -echo") passwd = raw_input(prompt) finally: os.system("stty echo") return passwd Strictly speaking, os.system is deprecated and you should use the equivalent invocation of subprocess.call: import subprocess def getpassword(prompt="Password: "): try: subprocess.call(["stty", "-echo"]) passwd = raw_input(prompt) finally: subprocess.call(["stty", "echo"]) return passwd If you don't want to use an external process, use termios: import termios, sys def getpassword(prompt="Password: "): 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 These functions work on any Posix system (including Mac OSX), but not on Windows. Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list