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

作者:轻舟漫溯 时间:2022-12-22 06:44:18 

最近做接口对接,遇到了.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来设置

来源:https://www.cnblogs.com/nottobe/p/5718823.html

标签:python,suds,webservice
0
投稿

猜你喜欢

  • 小谈访客浏览器分辨率

    2007-10-18 13:12:00
  • lodash内部方法getData和setData实例解析

    2024-04-19 09:43:12
  • Python tabulate结合loguru打印出美观方便的日志记录

    2021-02-23 02:39:45
  • Go Ginrest实现一个RESTful接口

    2024-05-21 10:26:08
  • python画环形图的方法

    2023-02-12 09:54:24
  • 使用Python读取大文件的方法

    2022-02-18 00:43:35
  • python模块之paramiko实例代码

    2022-08-08 08:34:51
  • SQL中的开窗函数(窗口函数)

    2024-01-24 09:59:43
  • Python 日志管理模块Loguru的用法小结

    2023-02-22 15:45:16
  • MySQL优化教程之超大分页查询

    2024-01-28 08:30:57
  • pyqt5-tools安装失败的详细处理方法

    2021-08-22 21:34:00
  • 跟老齐学Python之开始真正编程

    2021-06-26 21:42:22
  • Python安装官方whl包和tar.gz包的方法(推荐)

    2022-04-27 05:37:47
  • Python对切片命名的实现方法

    2023-09-30 09:43:15
  • 聊聊Druid register mbean error的问题

    2022-09-15 19:09:00
  • Mysql 5.7 新特性之 json 类型的增删改查操作和用法

    2024-01-24 23:45:53
  • mysql数据校验过程中的字符集问题处理

    2024-01-28 15:16:16
  • vue使用微信JS-SDK实现分享功能

    2023-07-02 16:59:46
  • 你凭什么说你的网站用户体验好

    2011-03-31 17:08:00
  • 开展全面的网站评估

    2007-09-27 19:21:00
  • asp之家 网络编程 m.aspxhome.com