zeep - 禁用警告 "Forcing soap:address location to HTTPS"

zeep - disable warning "Forcing soap:address location to HTTPS"

我正在使用 zeep 包访问 https 上的一些 API,并且在每个连接上它都会打印出警告(对 stderr):

Forcing soap:address location to HTTPS

我做的一些搜索发现负责的行是 this, which implies that this the result of the logging level of the module. Changing the log level seems to require editing this file

这对我来说是一个糟糕的解决方案,因为我希望能够在 运行 时关闭此警告,因为使用此包的应用程序将是冻结的应用程序(exe)。

如果这是相关的,这些是显示该警告所需的最少行(尽管显然,这里的域是虚构的,用户和密码也是虚构的):

import zeep
client = zeep.CachingClient('https://api.somedomain.com/Services/ApiService.svc?singleWsdl')
client.service.VerifyLogin('user', 'pass')

我知道 zeep 客户端可以设置为不强制使用 https,但我认为这会降低连接的安全性? (毕竟,我在没有 https 的情况下以明文形式传递用户名和密码)

警告上下文管理器怎么样?

你可以这样做,我以前用过

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)


经过几天的研究,我终于能够自己解决这个问题。我没有意识到可以从导入的模块更改日志记录级别。我在代码的开头添加了这一行(导入后)并解决了问题:

import logging
logging.getLogger('zeep').setLevel(logging.ERROR)

希望这对遇到同样问题的其他人有所帮助

如果你想更具体一点的日志级别开关,为了不遗漏整个zeep模块中的其他警告,你可以像下面这样设置:

logging.getLogger('zeep.wsdl.bindings.soap').setLevel(logging.ERROR)

这将只阻止 soap 绑定 class 的警告消息,但不会阻止其余的。