温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python使用suds调用webservice接口的方法

发布时间:2020-09-25 20:49:29 来源:脚本之家 阅读:160 作者:轻舟漫溯 栏目:开发技术

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

1.安装suds

  mac: sudo pip install suds

  linux: easy_install suds

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

2. 引用初始化

>>> from suds.client import Client
>>> url = 'http://www.gpsso.com/webservice/kuaidi/kuaidi.asmx?wsdl'
>>> client = Client(url)
>>> print client

Suds ( https://fedorahosted.org/suds/ ) version: 0.4 GA build: R699-20100913

Service ( Kuaidi ) tns="http://gpsso.com/"
Prefixes (1)
ns0 = "http://gpsso.com/"
Ports (2):
(KuaidiSoap)
Methods (1):
KuaidiQuery(xs:string Compay, xs:string OrderNo, )
Types (1):
ApiSoapHeader
(KuaidiSoap12)
Methods (1):
KuaidiQuery(xs:string Compay, xs:string OrderNo, )
Types (1):
ApiSoapHeader
>>>

对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来设置

以上这篇python使用suds调用webservice接口的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI