對於python僅作為用戶端調用webservice的情況,推薦使用suds庫來完成,比起zsi,soapy之類,它可以說是相當輕量級,使用非常方便。
安裝suds建議使用easy_insall來做。下面是官方的一些例子:
Python代碼 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.簡單參數調用
Python代碼 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.複雜參數
Java代碼 person = client.factory.create('Person') print person
Java代碼 (Person)= { phone = [] age = NONE name(Name) = { last = NONE first = NONE } }
#設定變數 Java代碼 phone = client.factory.create('Phone') phone.npa = 202 phone.nxx = 555 phone.number = 1212 Python代碼 name = client.factory.create('Name') name.first = 'Elmer' name.last = 'Fudd'
Python代碼 person.name = name person.age = 35 person.phone = [phone]