在 Python 2.7 中捕获警告而不停止部分程序
Catch Warning in Python 2.7 Without Stopping Part of Progam
我目前正在开发一个 Python 程序,该程序涉及使用用户名和密码登录。我正在使用 getpass
模块来完成此操作。我的问题是,在 getpass 无法控制 echo 的情况下,它会在继续执行程序之前将以下内容喷入终端。
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.
我想做的是捕获警告并打印我自己的自定义消息。我能想到的唯一代码是以下代码,它阻止显示回溯但根本不打印自定义消息。
import getpass
try:
getpass.getpass("Password: ")
except getpass.GetPassWarning:
print "Oh no!"
输出:
Warning: Password input may be echoed.
Password:
我想用我自己的信息替换文本 Warning: Password input may be echoed.
。
getpass
使用 python 内置模块 warnings
来显示此警告消息。您可以使用各种方法 filter/ignore 它们 (Python Docs / PyMOTW)。
您可以这样捕捉警告:
import getpass
import warnings
# the context manager resets the original
# filterwarnings after it has exited
with warnings.catch_warnings():
# this will raise warnings of type (or inherited of type)
# 'getpass.GetPassWarning' in the module 'getpass'
warnings.filterwarnings(
'error',
category=getpass.GetPassWarning,
module='getpass'
)
try:
password = getpass.getpass('Password: ')
except getpass.GetPassWarning:
print 'Cannot get password on insecure platform'
@PM2Ring 推荐的方法似乎是目前我找到的最好的解决方案。为了路过的人和回答问题的目的,我把所有的东西都包起来了post。
以下方法覆盖了 getpass
模块中的 fallback_getpass
函数,允许人们准确控制在需要回退时发生的事情。 有点 非常笨拙,但它完成了工作。
import getpass
def custom_fallback(prompt="Password: ",stream=None): #Clone of parameters from getpass.fallback_getpass
print "Custom message." #Custom message goes here
return getpass._raw_input(prompt) #Use getpass' custom raw_input function for security
getpass.fallback_getpass = custom_fallback #Replace the getpass.fallback_getpass function with our equivalent
password = getpass.getpass("Password: ") #Prompt for password
输出:
Custom message.
Password:
我目前正在开发一个 Python 程序,该程序涉及使用用户名和密码登录。我正在使用 getpass
模块来完成此操作。我的问题是,在 getpass 无法控制 echo 的情况下,它会在继续执行程序之前将以下内容喷入终端。
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.
我想做的是捕获警告并打印我自己的自定义消息。我能想到的唯一代码是以下代码,它阻止显示回溯但根本不打印自定义消息。
import getpass
try:
getpass.getpass("Password: ")
except getpass.GetPassWarning:
print "Oh no!"
输出:
Warning: Password input may be echoed.
Password:
我想用我自己的信息替换文本 Warning: Password input may be echoed.
。
getpass
使用 python 内置模块 warnings
来显示此警告消息。您可以使用各种方法 filter/ignore 它们 (Python Docs / PyMOTW)。
您可以这样捕捉警告:
import getpass
import warnings
# the context manager resets the original
# filterwarnings after it has exited
with warnings.catch_warnings():
# this will raise warnings of type (or inherited of type)
# 'getpass.GetPassWarning' in the module 'getpass'
warnings.filterwarnings(
'error',
category=getpass.GetPassWarning,
module='getpass'
)
try:
password = getpass.getpass('Password: ')
except getpass.GetPassWarning:
print 'Cannot get password on insecure platform'
@PM2Ring 推荐的方法似乎是目前我找到的最好的解决方案。为了路过的人和回答问题的目的,我把所有的东西都包起来了post。
以下方法覆盖了 getpass
模块中的 fallback_getpass
函数,允许人们准确控制在需要回退时发生的事情。 有点 非常笨拙,但它完成了工作。
import getpass
def custom_fallback(prompt="Password: ",stream=None): #Clone of parameters from getpass.fallback_getpass
print "Custom message." #Custom message goes here
return getpass._raw_input(prompt) #Use getpass' custom raw_input function for security
getpass.fallback_getpass = custom_fallback #Replace the getpass.fallback_getpass function with our equivalent
password = getpass.getpass("Password: ") #Prompt for password
输出:
Custom message.
Password: