5

我有一个供应商提供的网络服务;特定操作的 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>

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

我在执行 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)

但它引发了一个例外

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

任何

options_type=client.get_type('ns0:ArrayOf_soapenc_string')
options=options_type('recurseContainerHierarchy')
client.service.initExportDevice(filter, options)

或者

factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string(['recurseContainerHierarchy'])
client.service.initExportDevice(filter=filter, options=options)

或者

factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string('recurseContainerHierarchy')
client.service.initExportDevice(filter=filter, options=options)

或者

factory = client.type_factory('ns0')
options=factory.ArrayOf_soapenc_string(_value_1=['recurseContainerHierarchy'])
client.service.initExportDevice(filter=filter, options=options)

都引发相同的异常


options_type=client.get_type('ns0:ArrayOf_soapenc_string')
options=xsd.AnyObject(options_type, ['recurseContainerHierarchy'])
client.service.initExportDevice(filter, options)

产量

argument of type 'AnyObject' is not iterable

我如何构造这个参数?

4

1 回答 1

7

好的,所以我在使用 Zeep 时也遇到了麻烦(很容易使用 suds),问题是 Zeep 将数组作为函数返回(来自我的测试),因此您需要将函数分配给数组,然后对其进行修改。从您当前的代码中,看起来好像您正在将数据直接传递给函数(它不会存储它)。

使用上面的示例,下面应该检索 Array 类型并允许您将其修改为有效的数据类型。

emptyArrayPlaceholder = client.get_type('ns0:ArrayOf_soapenc_string')

Zeep 然后将此类型作为函数返回,因此首先您需要将此函数分配给一个变量,例如:

options = emptyArrayPlaceholder()

如果你要检查选项,你会看到它是一个字典,里面有你的列表。

print (options)
{'soapenc': []}

然后,您可以使用以下方法轻松地将项目添加到数组中:

options['soapenc'].append('Foo')

然后,您应该能够提交您的客户

client.service.initExportDevice(filter, options)

由于 options 现在是有效的 Zeep 数据类型。

于 2017-06-28T08:26:45.723 回答