I'm currently working on a Python program which involves logging in with a username and password. I'm using the getpass
module to accomplish this. My problem is that, in the case that getpass is unable to control echo, it spews the following into the terminal before continuing with the program.
Warning (from warnings module):
File "C:\Python27\lib\getpass.py", line 92
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
What I'd like to do is catch the warning and print my own custom message instead. The only code I can think of is the following which prevents the traceback from being shown but does not print the custom message at all.
import getpass
try:
getpass.getpass("Password: ")
except getpass.GetPassWarning:
print "Oh no!"
Output:
Warning: Password input may be echoed.
Password:
I'd like to replace the text, Warning: Password input may be echoed.
, with my own message ideally.