python - 如何使用 wsdl 文件创建异步 zeep 客户端?
问题描述
我有使用 zeep 创建肥皂客户端的代码。我的服务器不返回 wsdl 文件,但我在本地拥有它。
sycronous 版本的工作方式如下所示:
import uuid
from os import path
import structlog
import zeep
logger = structlog.get_logger(__name__)
class SyncClient(object):
def __init__(self, ip_address: str):
self.ip_address = ip_address
self.port = 8080
self.soap_client = None
self.corrupt_timeseries_files = []
self.id = uuid.uuid4()
def connect_soap_client(self):
this_files_dir = path.dirname(path.realpath(__file__))
wsdl = 'file://{}'.format(path.join(this_files_dir, 'SOAPInterface.wsdl'))
transport = zeep.Transport(timeout=5, operation_timeout=3)
client = zeep.Client(wsdl, transport=transport)
location = "http://{}:{}".format(self.ip_address, str(self.port))
self.soap_client = client.create_service("{urn:webservices}SOAPInterface", location)
然后 asyc 客户端看起来像这样:
class AsyncClient(object):
def __init__(self, ip_address: str):
self.ip_address = ip_address
self.port = 8080
self.soap_client: zeep.client.Client = None
self.corrupt_timeseries_files = []
self.id = uuid.uuid4()
def connect_soap_client(self):
this_files_dir = path.dirname(path.realpath(__file__))
wsdl = 'file://{}'.format(path.join(this_files_dir, 'SOAPInterface.wsdl'))
transport = zeep.transports.AsyncTransport(timeout=5, wsdl_client=wsdl, operation_timeout=3)
client = zeep.AsyncClient(wsdl, transport=transport)
location = "http://{}:{}".format(self.ip_address, str(self.port))
self.soap_client = client.create_service("{urn:webservices}SOAPInterface", location)
我已经看到 zeep 的文档指出文件加载是同步的。但是当我有本地文件时,我不明白如何创建异步客户端......
我在测试中运行代码时出现错误消息:
httpx.UnsupportedProtocol:不支持的 URL 协议“文件”
解决方案
在通过zeep和httpx源码调试之后,我发现解决方案其实很简单:
不指定file://{path}
,只指定{path}
。然后 WSDL 加载正常。
推荐阅读
- shell - 在 Applescript 中删除部分字符串
- javascript - 使用 Await 和 Async 包装 browser.WaitUntil()
- javascript - 键码上的重定向功能
- vba - 从特定电子邮件帐户发送时自动“代表发送”
- android - getInstance() 不适用于实时数据库中除 us-central1 之外的其他位置
- java - 替换二维数组中的某些单词
- user-interface - 新的 Firefox 更新菜单书签填充间距修复?
- r - if (is.na(n) || n > 65536L) stop("size 不能为 NA 也不能超过 65536") 中的错误:缺少 TRUE/FALSE 需要的值
- r - 计算两列之间的差异时出错
- python - KNN 在 r2 上缺少一致性