Python的WebService客户端:Suds
"Suds"是一个轻量级,Python的SOAP Web service 客户端。
对于python仅作为客户端调用webservice的情况,推荐使用suds库来完成,比起zsi,soapy之类,它可以说是相当轻量级,使用非常方便。
安装suds建议使用easy_insall来做。下面是官方的一些例子:
from suds.client import Client url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl' client = Client(url) #查看该service提供的方法 print client Suds - version: 0.3.3 build: (beta) R397-20081121 Service (WebServiceTestBeanService) tns="http://test.server.enterprise.rhq.org/" Prefixes (1): ns0 = "http://test.server.enterprise.rhq.org/" Ports (1): (Soap) Methods: addPerson(Person person, ) echo(xs:string arg0, ) getList(xs:string str, xs:int length, ) getPercentBodyFat(xs:string name, xs:int height, xs:int weight) getPersonByName(Name name, ) hello() testExceptions() testListArg(xs:string[] list, ) testVoid() updatePerson(AnotherPerson person, name name, ) Types (23): Person Name Phone AnotherPerson
1.简单参数调用
result = client.service.getPercentBodyFat('jeff', 68, 170) print result result = client.service.getPercentBodyFat(name='jeff', height=68, weight=170) print result #词典 d = dict(name='jeff', height=68, weight=170) result = client.service.getPercentBodyFat(**d) print result You have 21% body fat.
2.复杂参数
person = client.factory.create('Person') print person
(Person)= { phone = [] age = NONE name(Name) = { last = NONE first = NONE } }
#设置变量
phone = client.factory.create('Phone') phone.npa = 202 phone.nxx = 555 phone.number = 1212
name = client.factory.create('Name') name.first = 'Elmer' name.last = 'Fudd'
person.name = name person.age = 35 person.phone = [phone] #或者 person.phone.append(phone)
try: person_added = client.service.addPerson(person) except WebFault, e: print e
在0.3.8以上版本还提供了更简单的调用方式,完美的json
person = {} #根据对象结构构造json phone = { 'npa':202, 'nxx':555, 'number':1212, } name = { 'first':'Elmer', 'last':'Fudd' } person['name'] = name person['age'] = 35 person['phone'] = [phone,] try: person_added = client.service.addPerson(person) except WebFault, e: print e
3.异常处理
client = client(url, faults=False) result = client.service.addPerson(person) print result ( 200, person ...)https://bitbucket.org/jurko/suds
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!