在我的应用程序中,我需要访问内部(公司)SOAPAPI.到目前为止,我使用的是Zeep.但现在访问必须通过代理,并且必须将API的实际地址转换为代理的虚拟地址.
创建Zeep客户端也可以正常工作,我可以访问wsdl文件.但是,在调用相应的服务时会出现问题,因为Zeep从wsdl文件中获取相应的URL,而这与代理的虚拟地址不匹配.
下面我将try 用我的具体代码来说明这个问题:
假设SOAPAPI的地址是https://original-soap/wsdl?service=<service_name>
.
在代理中有一个从https://origial-soap
到http://virtual-soap
的映射.
所以Zeep应该使用的地址是http://virtual-soap/wsdl?service=<service_name>
.
我按如下方式初始化我的Zeep客户端:
from requests.auth import HTTPBasicAuth
from requests import Session
from zeep import Client
from zeep.transports import Transport
from zeep.helpers import serialize_object
session = Session()
session.proxies = {
'http': 'http://<proxy_host>:<proxy_port>',
'https': 'http://<proxy_host>:<proxy_port>',
}
proxy_header = {
"Proxy-Authorization": "Bearer <proxy_access_token>"
}
session.headers.update(proxy_header)
session.verify = False
session.auth = HTTPBasicAuth(<soap_user>, <soap_password>)
transport = Transport(session=session)
client = Client(wsdl='http://virtual-saop/wsdl?service=<service_name>', transport=transport)
print('CLIENT INITIALIZED') # <-- This print command is executed
soap_result = client.service['<service_function_name>'](<function parameters>) # <-- Connectivity errors occur here
因此,我的问题是如何更改Zeep在调用服务时使用的URL,以便在这里也使用虚拟地址.
提前感谢您的帮助!
多亏了@Bogdan,我使用以下服务初始化代码解决了这个问题:
service = client.create_service(
client.service._binding.name, client.service._binding_options['address'].replace('https://original-soap:443', 'http://virtual-soap:80', 1)
)