Python之Suds库调用WCF时复杂参数序列化
今年主要做自动化测技术支持工作,最近一直在做接口自动化这块,前些天在研究将web页面模拟http进行接口自动化,这周杭州那边想测试WCF服务,所以这两天一直在探索。遇到的第一个问题就是服务参数传参序列化的问题,怎么让python这边创建的对象能被WCF识别到。正好在大学的时候也学了WCF,不过一直都没用过,这次算是重温一下,用的都是一些WCF基础。
一、WCF服务准备
1.定义契约Contract
这里IServiceDemo.cs定义了服务契约IServiceDemo,并定义了几个操作契约OperationContract,5个操作契约传的参数不同,用来做测试,同时自定义了两个数据契约DataContract.并在ServiceDemo.svc中实现了上面操作契约。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IServiceDemo
{ [OperationContract]
string GetSimpleData(string value); [OperationContract]
List<Item> GetListData(List<Item> items); [OperationContract]
Item GetModelData(Item item); [OperationContract]
Dictionary<string,string> GetDicData(Dictionary<string,string> dic); [OperationContract]
Dictionary<string, Dictionary<string,int>[]> GetDicDicData(Dictionary<string, Dictionary<string, int>[]> dic); }
[DataContract]
public class ItemMenu
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Value { get; set; }
}
[DataContract]
public class Item
{
[DataMember]
public List<ItemMenu> ItemMenus { get; set; }
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace WcfServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
public class ServiceDemo : IServiceDemo
{ public string GetSimpleData(string value)
{
return value;
}
public List<Item> GetListData(List<Item> items)
{
return items;
}
public Item GetModelData(Item item)
{
return item;
}
public Dictionary<string, string> GetDicData(Dictionary<string, string> dic)
{
return dic;
}
public Dictionary<string, Dictionary<string, int>[]> GetDicDicData(Dictionary<string, Dictionary<string, int>[]> dic)
{
return dic;
} }
}
2.定义宿主
WCF宿主可以有多种方式,这里用了控制台应用程序来作为宿主,主要是想着做demo,可以发给测试,用控制台不用像iis那样部署了。在控制台应用程序的App.config中配置wcf服务。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceDemo.ServiceDemo" behaviorConfiguration="ServiceDemoBehavior" >
<endpoint address="" contract="WcfServiceDemo.IServiceDemo" binding="basicHttpBinding"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/ServiceDemo/"></add>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceDemoBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
3.启动服务
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(WcfServiceDemo.ServiceDemo)))
{
host.Open();
Console.WriteLine("服务已开启");
Console.Read();
}
}
4.出现的问题
在启动服务的时候,报了:HTTP 无法注册 URL http://+:8001/ServiceDemo/。进程不具有此命名空间的访问权限的错误。解决方法是VS2015用管理员打开就好了。

二.suds.client的使用
1.了解WCF
要调用WCF,首先得知道服务中有哪些参数,每个参数具体是什么类型。可以使用sud.client实例化client,然后打印出来看服务里面的内容。
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8') from suds.client import Client if __name__ == '__main__': client=Client('http://localhost:8001/ServiceDemo/?singleWsdl')
print client
# -----------------简单类型---------------------------
result= client.service.GetSimpleData('')
print result
Service ( ServiceDemo ) tns="http://tempuri.org/"
Prefixes (4)
ns0 = "http://schemas.datacontract.org/2004/07/WcfServiceDemo"
ns1 = "http://schemas.microsoft.com/2003/10/Serialization/"
ns2 = "http://schemas.microsoft.com/2003/10/Serialization/Arrays"
ns3 = "http://tempuri.org/"
Ports (1):
(BasicHttpBinding_IServiceDemo)
Methods (5):
GetDicData(ns2:ArrayOfKeyValueOfstringstring dic, )
GetDicDicData(ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 dic, )
GetListData(ns0:ArrayOfItem items, )
GetModelData(ns0:Item item, )
GetSimpleData(xs:string value, )
Types (11):
ns2:ArrayOfArrayOfKeyValueOfstringint
ns0:ArrayOfItem
ns0:ArrayOfItemMenu
ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1
ns2:ArrayOfKeyValueOfstringint
ns2:ArrayOfKeyValueOfstringstring
ns0:Item
ns0:ItemMenu
ns1:char
ns1:duration
ns1:guid

2.参数序列化
对于基础类型的参数可以直接传参,但复杂类型参数就比较麻烦了,怎么样在python定义的参数能在wcf服务端识别出来,也就是序列化反序列化的问题,例如GetDicDicData方法中要传递ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1类型的参数,在python中怎么定义呢,这个类型里面包含哪些属性,怎么实例化这个参数,可以使用client.factory.create('参数类型名')来创建,有时类型下面还有子类,所以在传参数时要弄清楚对象里面子类的数据类型,从根到叶子,而在实例化参数时需要从叶子到根来组装成对象。还有获取结果后获取解析的问题,这个把结果打印出来后可以一层一层的获取值。也可以调用last_received()方法,返回的是xml,然后用xpath解析。
print client.factory.create('ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
print client.factory.create('ns2:KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
print client.factory.create('ns2:ArrayOfArrayOfKeyValueOfstringint')
print client.factory.create('ns2:ArrayOfKeyValueOfstringint')
print client.factory.create('ns2:KeyValueOfstringint')
KeyValueOfstringint=client.factory.create('ns2:KeyValueOfstringint')
KeyValueOfstringint.Key='cyw'
KeyValueOfstringint.Value = 1
ArrayOfKeyValueOfstringint=client.factory.create('ns2:ArrayOfKeyValueOfstringint')
ArrayOfKeyValueOfstringint.KeyValueOfstringint=[KeyValueOfstringint]
ArrayOfArrayOfKeyValueOfstringint=client.factory.create('ns2:ArrayOfArrayOfKeyValueOfstringint')
ArrayOfArrayOfKeyValueOfstringint.ArrayOfKeyValueOfstringint=[ArrayOfKeyValueOfstringint]
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = client.factory.create('ns2:KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.Key = 'cuiyw'
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.Value = ArrayOfArrayOfKeyValueOfstringint
ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = client.factory.create('ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1
具体实现
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8') from suds.client import Client if __name__ == '__main__': client=Client('http://localhost:8001/ServiceDemo/?singleWsdl')
print client
# -----------------简单类型---------------------------
result= client.service.GetSimpleData('')
print result # -----------------自定义类---------------------------
print client.factory.create('ns0:Item')
print client.factory.create('ns0:ArrayOfItemMenu')
print client.factory.create('ns0:ItemMenu')
ItemMenu=client.factory.create('ns0:ItemMenu')
ItemMenu.Name='Cyw'
ItemMenu.Value = 'Cuiyw'
ArrayOfItemMenu= client.factory.create('ns0:ArrayOfItemMenu')
ArrayOfItemMenu.ItemMenu=[ItemMenu,ItemMenu]
Item=client.factory.create('ns0:Item')
Item.ItemMenus=ArrayOfItemMenu
result= client.service.GetModelData(Item)
print result
print result.ItemMenus.ItemMenu[0].Name
print result.ItemMenus.ItemMenu[0].Value # -----------------自定义类列表---------------------------
print client.factory.create('ns0:ArrayOfItem')
ArrayOfItem =client.factory.create('ns0:ArrayOfItem')
ArrayOfItem.Item=[Item,Item]
result= client.service.GetListData(ArrayOfItem)
print result
print result.Item[0].ItemMenus.ItemMenu[0].Name
# -----------------字典类型---------------------------
print client.factory.create('ns2:ArrayOfKeyValueOfstringstring')
print client.factory.create('ns2:KeyValueOfstringstring')
KeyValueOfstringstring= client.factory.create('ns2:KeyValueOfstringstring')
KeyValueOfstringstring.Key=''
KeyValueOfstringstring.Value = 'cyw'
ArrayOfKeyValueOfstringstring=client.factory.create('ns2:ArrayOfKeyValueOfstringstring')
ArrayOfKeyValueOfstringstring.KeyValueOfstringstring=[KeyValueOfstringstring]
result= client.service.GetDicData(ArrayOfKeyValueOfstringstring)
print result.KeyValueOfstringstring[0].Key
print result.KeyValueOfstringstring[0].Value
# print client.last_received() # -----------------字典嵌套---------------------------
print client.factory.create('ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
print client.factory.create('ns2:KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
print client.factory.create('ns2:ArrayOfArrayOfKeyValueOfstringint')
print client.factory.create('ns2:ArrayOfKeyValueOfstringint')
print client.factory.create('ns2:KeyValueOfstringint')
KeyValueOfstringint=client.factory.create('ns2:KeyValueOfstringint')
KeyValueOfstringint.Key='cyw'
KeyValueOfstringint.Value = 1
ArrayOfKeyValueOfstringint=client.factory.create('ns2:ArrayOfKeyValueOfstringint')
ArrayOfKeyValueOfstringint.KeyValueOfstringint=[KeyValueOfstringint]
ArrayOfArrayOfKeyValueOfstringint=client.factory.create('ns2:ArrayOfArrayOfKeyValueOfstringint')
ArrayOfArrayOfKeyValueOfstringint.ArrayOfKeyValueOfstringint=[ArrayOfKeyValueOfstringint]
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = client.factory.create('ns2:KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.Key = 'cuiyw'
KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.Value = ArrayOfArrayOfKeyValueOfstringint
ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = client.factory.create('ns2:ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1')
ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1 = KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1
result= client.service.GetDicDicData(ArrayOfKeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1)
print result.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1[0].Key
print result.KeyValueOfstringArrayOfArrayOfKeyValueOfstringintty7Ep6D1[0].Value
三、遇到的问题
在上面配置WCF服务时我把终结点配置的绑定配置成wsHttpBinding,导致在python调用时出现下面的错误。当启动新实例启动服务时是可以的,但使用宿主就不行,昨天没找到解决方法,今天把昨天写的在自己电脑上重现了下还是出现这个问题,找了半天没想到还真解决了。
<endpoint address="" contract="WcfServiceDemo.IServiceDemo" binding="wsHttpBinding"></endpoint>
Exception: (415, u"Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.")
Python之Suds库调用WCF时复杂参数序列化的更多相关文章
- Python之Suds库调用WCF实现复杂参数序列化
今年主要做自动化测技术支持工作,最近一直在做接口自动化这块,前些天在研究将web页面模拟http进行接口自动化,这周杭州那边想测试WCF服务,所以这两天一直在探索.遇到的第一个问题就是服务参数传参序列 ...
- python使用suds来调用webservice
对于python仅作为客户端调用webservice的情况,推荐使用suds库来完成,比起zsi,soapy之类,它可以说是相当轻量级,使用非常方便. 安装suds建议使用easy_insall来做. ...
- 调用WCF时,调用已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定。
解决方案: 其实只要在客户端配置文件中加上如下紫色粗体属性( maxReceivedMessageSize): <?xml version="1.0" encoding=&q ...
- 解决一个通过 WebReference 调用 WCF 时自定义 DataContract 类参数提交的问题
先看一下VS2013自动创建默认的IService1.vb,注意自定义的数据契约 CompositeType ' 注意: 使用上下文菜单上的“重命名”命令可以同时更改代码和配置文件中的接口名“ISer ...
- python中调用函数时,参数顺序与参数赋值问题
设置类和函数如下:class MM(): def ff(self,url(1),method(2),data=None(3),cookie=None(4)): if method.lower()==& ...
- python不同包之间调用时提示文件模块不存在的问题
python对于跨包调用函数时,经常会提示模块不存在的问题,主要是python程序执行时,搜索路径导致的,python程序执行的路径依次是: (1)程序根目录(2)环境变量(3)标准库目标(D:\Py ...
- Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'
使用python的suds包调用webservice服务接口,报错:AttributeError: 'Document' object has no attribute 'set' 调用服务接口代码: ...
- 使用Python的Mock库进行PySpark单元测试
测试是软件开发中的基础工作,它经常被数据开发者忽视,但是它很重要.在本文中会展示如何使用Python的uniittest.mock库对一段PySpark代码进行测试.笔者会从数据科学家的视角来进行描述 ...
- 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法
关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...
随机推荐
- 高可用高性能分布式文件系统FastDFS实践Java程序
在前篇 高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备 中已介绍搭建高可用的分布式文件系统架构. 那怎么在程序中调用,其实网上有很多栗子, ...
- 98、vue.js简单入门
本篇导航: 介绍与安装 vue常用指令 一.介绍与安装 vue是一套构建用户界面的JAVASCRIPT框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层, ...
- jsp页面固定页面为绝对路径
1 <!-- 固定到绝对路径 --> 2 <base href="<%=request.getContextPath()%>/"/>
- 解决spring定时任务执行2次和tomcat部署缓慢的问题
spring定时任务执行2次 问题重现和解析 最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次.经过搜索发现是服务器上tomcat的配置文 ...
- PHP中file_exists()函数不能检测包含中文的文件名的解决办法
版权声明:本文为博主原创文章,未经博主允许不得转载. PHP中一般使用file_exists()判断某个文件或者文件夹是否存在,如果文件或文件夹存在则返回true,不存在则返回fal ...
- 【问题解决】java中为什么不建议使用DataInputStream 的readLine()方法
常用方法 int read(byte[] b) 从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中. int read(byte[] b, int off, int len) 从包含 ...
- protobuf java基础
1:定义proto文件: 以一个地址薄为例,从建立一个.proto文件开始,为需要序列化的数据接口加入一个message属性,在message里面,为每一个字段指定名称和类型(算是IDL吧),如下 ...
- P1345 [USACO5.4]奶牛的电信Telecowmunication
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
- Windows核心编程&进程
1. 进程的定义 说白了进程就是一个正在运行的执行程序,包含内核对象和独立的地址空间,内核对象负责统计和管理进程信息,地址空间包括所有可执行文件或DLL 模块的代码和数据.动态内存分配(线程堆和栈的分 ...
- 使用jQuery的ajax调用action的例子
直接使用ajax请求会比较繁琐,但是jQuery为我们提供了简单使用ajax的方法. 下面是一个在jQuery easyUI中,利用ajax请求,使下拉菜单关联文本框的例子.其中ajax请求就是8-1 ...