您的位置:首页 > 编程语言 > Python开发

python使用suds调用webservice

2016-12-20 13:40 316 查看
一、准备环境

  webservice接口测试,需要用到suds库,网上百度的各种suds库都没法安装,我这里的Python3.5版本,所以安装不了那些suds库也没有办法在线安装,所以这里就提供一个大家都可用的方法和suds库。

 

1)下载suds库,地址:http://pypi.python.org/packages/source/s/suds-jurko/suds-jurko-0.4.1.jurko.4.zip#md5=769689edca81c34c0421a4145b08c264,文件名为:suds-jurko-0.4.1.jurko.4.zip

2)解压压缩包,放到Python安装路径下的scripts文件夹下面。

3)然后在cmd命令行中,进入到suds-jurko-0.4.1.jurko.4文件下面,输入命令: python setup.py install

4)安装成功

5)注意:如果在py中写代码 import suds报错,就要把suds-jurko-0.4.1.jurko.4文件夹下面的dist、suds以及suds_jurko.egg-info这三个文件夹,拷贝到Lib 下面的site-packages路径下面,就可以正常访问了。

6)一切准备就绪,可以开始测试啦!

 

二、使用suds库来测试webservice接口


对于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
16146
, )  

         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)  



 

Java代码  



try:  

   person_added = client.service.addPerson(person)  

except WebFault, e:  

  print e  

 
  在0.3.8以上版本还提供了更简单的调用方式,完美的json


 

Python代码  



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.异常处理

Python代码  



client = client(url, faults=False)  

result = client.service.addPerson(person)  

print result  

( 200, person ...)  

 更多可以查看官方文档:https://fedorahosted.org/suds/wiki/Documentation,里面还讲了soap头得安全认证,webservice cache等高级话题,有需要可以查看,都比较详细。
转载自:http://blog.csdn.net/jjwspj/article/details/8086955
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: