www.cnblogs.com/ruiyqinrui

开源、架构、Linux C/C++/python AI BI 运维开发自动化运维。 春风桃李花 秋雨梧桐叶。“力尽不知热 但惜夏日长”。夏不惜,秋不获。@ruiY--秦瑞

python爬虫,C编程,嵌入式开发.hadoop大数据,桉树,onenebula云计算架构.linux运维及驱动开发.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

1.安装suds

  mac: sudo pip install suds

  linux: easy_install suds

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

2. 引用初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> from suds.client import Client
>>> 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)
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直接调用

1
2
3
4
5
6
7
8
9
>>> client.service.KuaidiQuery(Company='EMS', OrderNo='1111')
(KuaidiQueryResult){
 API =
  (API){
   RESULTS = "0"
   MESSAGE = "接口查询成功"
  }
 }
>>>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> 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方法设置

1
2
3
>>>
>>> client.set_options(soapheaders=[header,])
>>>
posted on 2020-05-13 11:17  秦瑞It行程实录  阅读(327)  评论(0编辑  收藏  举报
www.cnblogs.com/ruiyqinrui