您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關python中怎么使用suds調用webservice,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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]
#或者
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 ...)
以上就是python中怎么使用suds調用webservice,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。