Python通过suds调用WCF服务 传递Python List 到 WCF Service
来源:互联网 发布:sqlserver数据恢复 编辑:程序博客网 时间:2024/05/01 13:07
问题:
python传递list (or array, collection) of strings 到一个WCF service endpoint
The WCF interface:
[OperationContract]string[] TestList(IEnumerable<string> vals);
Binding in Web.config:
<endpoint address="http://localhost:13952/Service/Endpoint.svc" binding="basicHttpBinding" contract="Service.IEndpoint">
Python调用WCF服务:
from suds.client import Clienturl = 'http://localhost:13952/Service/Endpoint.svc?wsdl'client = Client(url)result = client.service.TestList(('a', 'b', 'c'))
错误:
suds.WebFault: Server raised fault: 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:vals. The InnerException message was 'Error in line 1 position 310. Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. '. Please see InnerException for more details.'
使用WireShark抓包,观察序列化后的soap数据,发现list序列化有错误,导致服务端无法反序列化。
解决方案:
使用Suds client factory。此步骤非常重要:
client.factory.create('ArrayOfString')
示例如下:
#!/usr/bin/env pythonimport unittestimport sudsfrom suds.client import Clientclass TestCSharpWebService(unittest.TestCase): def setUp(self): url = "http://localhost:8080/WebService.asmx?wsdl=0" self.client = Client(url) def test_service_definition(self): # You can print client to see service definition. self.assertTrue("orderAlphabetically" in self.client.__str__()) self.assertTrue("ArrayOfString" in self.client.__str__()) def test_orderAlphabetically_service(self): # Instanciate your type using the factory and use it. ArrayOfString = self.client.factory.create('ArrayOfString') ArrayOfString.string = ['foo', 'bar', 'foobar', 'a', 'b', 'z'] result = self.client.service.orderAlphabetically(ArrayOfString) # The result list contains suds.sax.text.Text which acts like a string. self.assertEqual( type(result.string[0]), suds.sax.text.Text) self.assertEqual( [str(x) for x in result.string], ['a', 'b', 'bar', 'foo', 'foobar', 'z'])if __name__ == '__main__': unittest.main()
web service:
namespace WebServiceMono{ using System.Linq; using System.Web.Services; using System.Collections.Generic; public class WebService : System.Web.Services.WebService { [WebMethod] public string[] orderAlphabetically (List<string> list) { var result = list.OrderBy (s => s); return result.ToArray (); } }}
使用client.factory.create('ArrayOfString')
后可以很好的解决python list序列化问题。 0 0
- Python通过suds调用WCF服务 传递Python List 到 WCF Service
- python调用wcf服务
- Python调用Web Service(使用suds)
- python调用wcf服务 实现网站对客户端的调用
- jQuery调用WCF服务传递JSON对象
- jQuery调用WCF服务传递JSON对象
- 通过jsonp调用wcf服务的例子
- python使用suds调用webservice
- python使用suds调用webservice
- python使用SUDS调用webservice
- python使用suds调用webservice
- python使用suds调用webservice
- 会话WCF服务 -> C# WCF COM 客户端 双工通信 -> C++通过COM调用C# WCF客户端
- WindowsMobile调用WCF服务
- Silverlight调用WCF服务
- javascript调用WCF服务
- ajax调用WCF服务
- iPhone调用WCF服务
- 随机过程的参考书籍
- Path Sum II
- Thinking in Java 第四章、第五章
- 单点登录
- 搜索技术概述
- Python通过suds调用WCF服务 传递Python List 到 WCF Service
- Eclipse常见错误及解决方案
- CentOS7 上源码安装KVM(qemu--kvm)
- ES6新特性概览
- 我的Android系统学习笔记——HAL
- Course Schedule
- MessageBox in iOS
- 【算法】最大连续乘积子串
- 黑马程序员 安卓基础学习笔记