python使用suds调用webservice接口

  最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了

1.安装suds

  mac: sudo pip install suds

  linux: easy_install suds

  也可以通过去官网下载suds代码,再本地安装

2. 引用初始化

 1 >>> from suds.client import Client
 2 >>> url = 'http://www.gpsso.com/webservice/kuaidi/kuaidi.asmx?wsdl'
 3 >>> client = Client(url)
 4 >>> print client
 5 
 6 Suds ( https://fedorahosted.org/suds/ ) version: 0.4 GA build: R699-20100913
 7 
 8 Service ( Kuaidi ) tns="http://gpsso.com/"
 9 Prefixes (1)
10 ns0 = "http://gpsso.com/"
11 Ports (2):
12 (KuaidiSoap)
13 Methods (1):
14 KuaidiQuery(xs:string Compay, xs:string OrderNo, )
15 Types (1):
16 ApiSoapHeader
17 (KuaidiSoap12)
18 Methods (1):
19 KuaidiQuery(xs:string Compay, xs:string OrderNo, )
20 Types (1):
21 ApiSoapHeader
22 >>>

  对url做一下说明,一般要确认给的wsdl地址是正常模式,地址打开一般为xml格式而有些服务是做成了html模式,这个会导致实例化或者调用方法的时候出现xml解析异常。

3. 方法调用

  2中的client打印出来就可以知道,该webserviece服务定义了什么方法,方法需要什么参数,声明了什么信息等(如头信息,ApiSoapHeader),方法可以通过client.serviece直接调用

>>> client.service.KuaidiQuery(Company='EMS', OrderNo='1111')
(KuaidiQueryResult){
   API =
      (API){
         RESULTS = "0"
         MESSAGE = "接口查询成功"
      }
 }
>>>

  而声明的头信息,则可以用factory的方式去实例化

>>> header = client.factory.create('ApiSoapHeader')
>>> print header
(ApiSoapHeader){
   APICode = None
   APIKey = None
 }
>>> header.APICode = '123'
>>> header.APIKey = 'key123'
>>> print header
(ApiSoapHeader){
   APICode = "123"
   APIKey = "key123"
 }
>>>

  头信息需要用set_options方法设置

>>>
>>> client.set_options(soapheaders=[header,])
>>>

  如果没有描述的头信息,可以通过查阅文档https://fedorahosted.org/suds/wiki/Documentation查询custom soap headers来设置

4. 在此附上参考朋友的文章http://www.cnblogs.com/Tommy-Yu/p/5567091.html

 

转载请注明本文来源:http://www.cnblogs.com/nottobe/p/5718823.html

谢谢

 

转载于:https://www.cnblogs.com/nottobe/p/5718823.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值