[英]Catch Warning in Python 2.7 Without Stopping Part of Progam
I'm currently working on a Python program which involves logging in with a username and password. 我目前正在开发一个Python程序,它涉及使用用户名和密码登录。 I'm using the
getpass
module to accomplish this. 我正在使用
getpass
模块来实现这一目标。 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. 我的问题是,在getpass无法控制回声的情况下,它会在继续执行程序之前将以下内容喷入终端。
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.
我想替换文本,
Warning: Password input may be echoed.
, with my own message ideally. ,理想地用我自己的信息。
getpass
uses the python builtin module warnings
to show this warning message. getpass
使用python内置模块warnings
来显示此警告消息。 You can filter/ignore them using various methods ( Python Docs / PyMOTW ). 您可以使用各种方法过滤/忽略它们( Python Docs / PyMOTW )。
You could catch the warning like so: 您可以像这样捕获警告:
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'
The method suggested by @PM2Ring appears to be the best solution I've found so far. @ PM2Ring建议的方法似乎是我迄今为止发现的最佳解决方案。 For the sake of others passing by and for the purpose of answering the question, I'll wrap everything up in this post.
为了别人路过并为了回答这个问题,我将在这篇文章中把所有内容都包括在内。
The following method overwrites the fallback_getpass
function in the getpass
module, allowing one to control exactly what happens in the event of the fallback being required. 以下方法将覆盖
getpass
模块中的fallback_getpass
函数,从而可以准确控制在需要回退时发生的情况。 It's a little very hacky but it gets the job done. 它
有点非常hacky但它完成了工作。
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
Output: 输出:
Custom message.
Password:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.