我正在使用zeep
包访问https上的某些API,在每个连接上它都会打印出警告(到stderr(:
Forcing soap:address location to HTTPS
我做了一些搜索,发现负责的行是这个,这意味着这是模块日志级别的结果。更改日志级别似乎需要编辑此文件。
这对我来说是一个糟糕的解决方案,因为我希望能够在运行时关闭此警告,因为使用此包的应用程序将是一个冻结的应用程序(exe(。
如果这是相关的,这些是显示警告所需的最少行(尽管很明显,这里的域是虚构的,用户和密码也是虚构的(:
import zeep
client = zeep.CachingClient('https://api.somedomain.com/Services/ApiService.svc?singleWsdl')
client.service.VerifyLogin('user', 'pass')
我知道zeep
客户端可以设置为不强制https,但我认为这会降低连接的安全性吗?(毕竟,我将用户名和密码作为明文传递,而不使用https(
经过几天的研究,我终于能够自己解决这个问题。我没有意识到日志记录级别可以从导入的模块更改。我在代码的开头添加了这一行(导入后(,它解决了问题:
import logging
logging.getLogger('zeep').setLevel(logging.ERROR)
希望这能帮助其他遇到同样问题的人
如果你想对日志级别开关更具体一点,为了不错过zeep模块中的其他警告,你可以如下设置:
logging.getLogger('zeep.wsdl.bindings.soap').setLevel(logging.ERROR)
这将防止只针对soap绑定类的警告消息,而不针对其他类
警告上下文管理器怎么样?
你可以做这样的事情,我在过去的中使用过
import zeep
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
# this filters all warnings, and the context manager records them
# your code is here:
client = zeep.CachingClient('https://api.somedomain.com/Services/ApiService.svc?singleWsdl')
client.service.VerifyLogin('user', 'pass')
# now you want to verify you didn't ignore a different warning
# here's an idea of how to verify:
assert len(w) == 1, "More than one warning caught!"
assert isinstance(w[0], WarningCategoryItShouldBe)