Zeep - 关闭主机验证

Zeep - turn off host verification

我正在使用 Python zeep to make a SOAP request. For SSL verification, HostHeaderSSLAdapter 添加为我的 URL https://dev_alias:8096/some_soap,包含别名而不是实际主机名。

以下带有 requests 的代码工作正常。所以我假设 session 是正确的。

import requests
from requests_toolbelt.adapters import host_header_ssl
from zeep.transports import Transport
from zeep import Client, Settings

url = 'https://dev_alias:8096/some_soap'
params = {'param1': 'value1'}

session = requests.Session()
session.verify = 'CertBundle.pem'
session.mount('https://', host_header_ssl.HostHeaderSSLAdapter())

headers = {'SOAPAction': '""', 'Content-Type': 'text/xml; charset=utf-8'}
message = b"<?xml version=\'1.0\' encoding=\'utf-8\'?>....</soap-env:Envelope>"

session.post(url, data=message, headers=headers)

但是,当相同的 session 用于 zeep 的 Transport 时,此代码失败。

transport = Transport(session=session)
client = Client(url, transport=transport)
client.service.some_service(params)

SSLError: HTTPSConnectionPool(host='actual_hostname', port=8096): 
Max retries exceeded with url: /some_soap (Caused by SSLError(CertificateError(
"hostname 'actual_hostname' doesn't match 'dev_alias'",),))

如何使用 zeep 关闭主机名验证?使用 zeep 版本 3.4.0 0 并请求 2.22.0

出于测试目的,您可以按照此处的说明禁用 ssl 验证 https://python-zeep.readthedocs.io/en/master/transport.html

在这种情况下,将 session.verify 行更改为

session.verify = False

请注意,您几乎肯定不想在生产系统中执行此操作。

聚会可能有点晚,但只是为了提供一个完整的代码片段来扩展 Andreas 的回答:

from zeep import Client
from zeep.transports import Transport

url = 'https://dev_alias:8096/some_soap'

# create a custom transport object turn off verification in it's session
transport = Transport()
transport.session.verify = False

client = Client(url, transport=transport)
client.service.some_service()

我自己在找出正确的语法时遇到了一些问题,希望这对您有所帮助。