您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)python中怎么使用suds調(diào)用webservice,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
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.簡(jiǎn)單參數(shù)調(diào)用
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.復(fù)雜參數(shù)
Java代碼
person = client.factory.create('Person')
print person
Java代碼
(Person)=
{
phone = []
age = NONE
name(Name) =
{
last = NONE
first = NONE
}
}
#設(shè)置變量
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以上版本還提供了更簡(jiǎn)單的調(diào)用方式,完美的json
person = {}
#根據(jù)對(duì)象結(jié)構(gòu)構(gòu)造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調(diào)用webservice,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。