python调用ironpython_IronPython调用WCF服务

WebService除了比较常见的soap,restful等形式外,在.net平台下比较多的是wcf服务。

具体介绍可以参看MSDN里的描述。

如何对wcf服务接口进行测试,除了写.net代码外,还可以通过ironpython来实现(谁让ironpython就是python在.net平台上实现呢)

调用wcf服务,就需要知道wcf服务的这三个方面:

契约 。wcf规定,契约必须要以接口的方式来体现,实际的服务代码必须要由这些合约接口派生并实现)

绑定。WCF 支持HTTP,TCP,Named Pipe,MSMQ,Peer-To-Peer TCP 等协议,而 HTTP 又分为基本 HTTP 支持 (BasicHttpBinding) 以及 WS-HTTP 支持 (WsHttpBinding),而 TCP 亦支持 NetTcpBinding,NetPeerTcpBinding 等通信方式。

安全性。

了解了wcf以上的内容后,就可以进行调用了。

比如有一个wcf服务(http_binding的),契约都定义在IUserService中,它里面有一个方法是tmethod.

需要调用该wcf服务中的tmethod方法

import clr

clr.AddReference('System.ServiceModel')

from System.ServiceModel import (ServiceHost, BasicHttpBinding,NetTcpBinding,

ServiceBehaviorAttribute, InstanceContextMode,Description,SecurityMode,WSHttpBinding)

try:

clr.AddReference('Services.Contract')

except Exception,ex:

raise AssertionError("%s" % ex)

from Services.Contract import IUserService

sec_mode = System.ServiceModel.NetTcpSecurity()

sec_mode.Mode = System.ServiceModel.SecurityMode.None

#绑定

def basic_http_binding():

http_binding = System.ServiceModel.BasicHttpBinding()

http_binding.TextEncoding = Text.Encoding.UTF8

http_binding.MaxBufferPoolSize = 52428800

http_binding.ReceiveTimeout=TimeSpan(0, 1, 30)

http_binding.MaxReceivedMessageSize = 52428800

return http_binding

class TestWcf(object):

def __init__(self, service_url):

self.service_url = service_url

#createchannel的方式和C#类似,主要是ChannelFactory的使用

def __create_channel(self):

mycf = System.ServiceModel.ChannelFactory[IUserService](

basic_http_binding(),

System.ServiceModel.EndpointAddress(self.service_url))

wcfcli = mycf.CreateChannel()

return wcfcli

def test_tmethod(self):

rst = self.__create_channel().tmethod()

print rst

然后就可以实例化TestWcf类,然后调用test_tmethod来获取对应服务的返回值了

上面使用的是http_binding的形似,使用net_tcp_bing的一样的

sec_mode = System.ServiceModel.NetTcpSecurity()

sec_mode.Mode = System.ServiceModel.SecurityMode.None

def basic_net_tcp_binding():

tcp_binding = System.ServiceModel.NetTcpBinding()

tcp_binding.MaxBufferPoolSize = 52428800

tcp_binding.ReceiveTimeout=TimeSpan(0, 1, 30)

tcp_binding.MaxReceivedMessageSize = 52428800

tcp_binding.Security = sec_mode

return tcp_binding

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值