Dou*_*eid 5 python wsdl web-services zeep
我有一个供应商提供的网络服务;特定操作的 WSDL 如下所示:
<complexType name="ArrayOf_soapenc_string">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="soapenc:string[]"/>
</restriction>
</complexContent>
</complexType>
...
<wsdl:message name="initExportDeviceRequest">
<wsdl:part name="filter" type="soapenc:string"/>
<wsdl:part name="options" type="impl:ArrayOf_soapenc_string"/>
</wsdl:message>
...
<wsdl:operation name="initExportDevice" parameterOrder="filter options">
<wsdl:input message="impl:initExportDeviceRequest" name="initExportDeviceRequest"/>
<wsdl:output message="impl:initExportDeviceResponse" name="initExportDeviceResponse"/>
</wsdl:operation>
Run Code Online (Sandbox Code Playgroud)
python -mzeep ipam_export.wsdl在 WSDL 上运行会产生以下结果:
Global types:
ns0:ArrayOf_soapenc_string(_value_1: string[], arrayType: xsd:string, offset: ns1:arrayCoordinate, id: xsd:ID, href: xsd:anyURI, _attr_1: {})
...
Service: ExportsService
Port: Exports (Soap11Binding: {http://diamondip.com/netcontrol/ws/}ExportsSoapBinding)
Operations:
...
initExportDevice(filter: ns1:string, options: {_value_1: string[], arrayType: xsd:string, offset: ns1:arrayCoordinate, id: xsd:ID, href: xsd:anyURI, _attr_1: {}}) -> initExportDeviceReturn: ns2:WSContext
Run Code Online (Sandbox Code Playgroud)
我在执行 initExportDevice 调用时遇到困难,特别是options参数。
How to use a complex type from a WSDL with zeep in Python向我建议这应该有效:
filter_type=client.get_type('ns1:string')
filter=filter_type('addrType=4')
options_type=client.get_type('ns0:ArrayOf_soapenc_string')
options=options_type(['recurseContainerHierarchy'])
client.service.initExportDevice(filter, options)
Run Code Online (Sandbox Code Playgroud)
但它引发了一个例外
Any element received object of type 'str', expected lxml.etree._Element or zeep.objects.string
See http://docs.python-zeep.org/en/master/datastructures.html#any-objects for more information
Run Code Online (Sandbox Code Playgroud)
任何
options_type=client.get_type('ns0:ArrayOf_soapenc_string')
options=options_type('recurseContainerHierarchy')
client.service.initExportDevice(filter, options)
Run Code Online (Sandbox Code Playgroud)
或者
factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string(['recurseContainerHierarchy'])
client.service.initExportDevice(filter=filter, options=options)
Run Code Online (Sandbox Code Playgroud)
或者
factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string('recurseContainerHierarchy')
client.service.initExportDevice(filter=filter, options=options)
Run Code Online (Sandbox Code Playgroud)
或者
factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string(_value_1=['recurseContainerHierarchy'])
client.service.initExportDevice(filter=filter, options=options)
Run Code Online (Sandbox Code Playgroud)
都引发相同的异常
options_type=client.get_type('ns0:ArrayOf_soapenc_string')
options=xsd.AnyObject(options_type, ['recurseContainerHierarchy'])
client.service.initExportDevice(filter, options)
Run Code Online (Sandbox Code Playgroud)
产量
argument of type 'AnyObject' is not iterable
Run Code Online (Sandbox Code Playgroud)
我如何构造这个参数?
好的,所以我在使用 Zeep 时也遇到了问题(很容易与 suds 一起使用),问题是 Zeep 将数组作为函数返回(来自我的测试),因此您需要将该函数分配给一个数组,然后对其进行修改。从您当前的代码来看,您似乎是将数据直接传递给函数(不会存储它)。
使用上面的示例,下面应该检索 Array 类型并允许您将其修改为有效的数据类型。
emptyArrayPlaceholder = client.get_type('ns0:ArrayOf_soapenc_string')
Run Code Online (Sandbox Code Playgroud)
Zeep 然后将此类型作为函数返回,因此首先您需要将此函数分配给一个变量,例如:
options = emptyArrayPlaceholder()
Run Code Online (Sandbox Code Playgroud)
如果您当时检查选项,您会看到它是一个字典,其中包含您的列表。
print (options)
{'soapenc': []}
Run Code Online (Sandbox Code Playgroud)
然后,您可以轻松地将项目添加到数组中:
options['soapenc'].append('Foo')
Run Code Online (Sandbox Code Playgroud)
然后你应该能够提交你的客户
client.service.initExportDevice(filter, options)
Run Code Online (Sandbox Code Playgroud)
As options 现在是有效的 Zeep 数据类型。