用zeep创建一个字符串数组参数?

2024-06-25 06:02:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个供应商提供的webservice;某个操作的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>

在WSDL上运行python -mzeep ipam_export.wsdl会产生以下结果:

^{pr2}$

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

如何构造这个参数?在


Tags: nameclientmessagestringfactorytypeservicefilter
1条回答
网友
1楼 · 发布于 2024-06-25 06:02:53

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

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

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

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

^{pr2}$

如果你再检查选项,你会看到它是一个dict,里面有你的列表。在

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

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

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

然后你应该能够向你的客户提交

client.service.initExportDevice(filter, options)

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

相关问题 更多 >