本篇文章给大家分享的是有关python中怎么使用suds调用webservice,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;网站设计、成都网站制作收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了十年的创新互联网站建设公司。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,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。