1.安装suds
mac: sudo pip install suds
linux: easy_install suds
也可以通过去官网下载suds代码,再本地安装
2. 引用初始化
1 >>>from suds.client import Client
2 >>>url = ''
3 >>>client = Client(url)
4 >>>print client
5
6 Suds ( ) version: 0.4 GA build: R699-20100913
7
8 Service ( Kuaidi ) tns=""
9 Prefixes (1)
10 ns0 = ""
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,])
>>>
方法如下:1. 如果安装了easy_install或pip, 用easy_install suds或pip install suds
2. 到http://pypi.python.org/pypi/suds下载压缩包, 解压,
在命令行下进入解压目录, 执行 python setup.py install
首先建议你安装一个扩展库安装工具,推荐easy_install工具,你可以在网上下载,也可以先去下载一个 ez_setup.py ,这个程序下载后用python.exe运行一下,会自动根据你的版本帮你下载和安装一个easy_install,目前只支持到python2.6,看来python3目前还是没有太多的公司在使用啊。。。
后面就可以用这个easy_install工具进行第三方库的下载了,比如我要下载soaplib这个库,可以执行easy_install soaplib,它会自己去相关网站查找这个库,以及这个库的依赖,如果你手工安装,那依赖会把你搞疯掉的
关于哪个库更适用做webservice
现在网上查到最多的是ZSI或者叫soappy,实际上05年之后就没有更新了,而且只支持到2.5,放弃
soaplib,这个目前2.0,还是不错的,不过手册不是太好读,只有server端的用法,client我没找到suds,这个我在用,用起来比较简单,示例代码如下:
[python] view plain copy
The library is now ready to use. We start by importing the suds library, creating a client based on a SOAP url, and asking the library to print the SOAP web service methods that are available to us.
import suds
url = "http://www.ecubicle.net/iptocountry.asmx?wsdl"
client = suds.client.Client(url)
print client
From the output of the last print command, we learn that there is a method called FindCountryAsString that takes one argument: the IP address.
print client.service.FindCountryAsString("194.145.200.104")
And it shows (edited for readability):
<?xml version="1.0"?>
<IPAddressService>
<country>Netherlands</country>
</IPAddressService>
Normally you want to have the contents of the SOAP body. This is what suds provides in a very elegant way. However, you’re a bit stuck when you want to get something from the SOAP header. The author of suds realised this and made a backdoor to get the information anyway. We start by showing what the function last_received contains:
print client.last_received()
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
<soap:Header>
<ResponseHeader xmlns="">
<resultCode>1000</resultCode>
<resultDescription>Success</resultDescription>
</ResponseHeader>
</soap:Header>
<soap:Body>
...
</soap:Body>
</soap:Envelope>
We can get portions of this data by doing some XML handling. Let’s say we want to print the resultCode:
print client.last_received().getChild("soap:Envelope").getChild("soap:Header")
.getChild("ResponseHeader").getChild("resultCode").getText()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)