×

Python Web Service

python使用suds调用webservice

Kalet Kalet 发表于2019-05-10 22:26:02 浏览223 评论0

抢沙发发表评论

 对于python仅作为客户端调用webservice的情况,推荐使用suds库来完成,比起zsi,soapy之类,它可以说是相当轻量级,使用非常方便。

  安装suds建议使用easy_insall来做。下面是官方的一些例子:

 

Python代码  收藏代码
  1. from suds.client import Client  

  2. url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl'  

  3. client = Client(url)  

  4.   

  5. #查看该service提供的方法  

  6. print client  

  7.   

  8. Suds - version: 0.3.3 build: (beta) R397-20081121  

  9.   

  10. Service (WebServiceTestBeanService) tns="http://test.server.enterprise.rhq.org/"  

  11.    Prefixes (1):  

  12.      ns0 = "http://test.server.enterprise.rhq.org/"  

  13.    Ports (1):  

  14.      (Soap)  

  15.        Methods:  

  16.          addPerson(Person person, )  

  17.          echo(xs:string arg0, )  

  18.          getList(xs:string str, xs:int length, )  

  19.          getPercentBodyFat(xs:string name, xs:int height, xs:int weight)  

  20.          getPersonByName(Name name, )  

  21.          hello()  

  22.          testExceptions()  

  23.          testListArg(xs:string[] list, )  

  24.          testVoid()  

  25.          updatePerson(AnotherPerson person, name name, )  

  26.    Types (23):  

  27.      Person  

  28.      Name  

  29.      Phone  

  30.      AnotherPerson  

 

   1.简单参数调用

 

Python代码  收藏代码
  1. result = client.service.getPercentBodyFat('jeff'68170)  

  2. print result  

  3.   

  4. result = client.service.getPercentBodyFat(name='jeff', height=68, weight=170)  

  5. print result  

  6.   

  7. #词典  

  8. d = dict(name='jeff', height=68, weight=170)  

  9. result = client.service.getPercentBodyFat(**d)  

  10. print result  

  11.   

  12. You have 21% body fat.  

 

   2.复杂参数

 

Java代码  收藏代码
  1. person = client.factory.create('Person')  

  2. print person  

 

Java代码  收藏代码
  1. (Person)=  

  2.   {  

  3.     phone = []  

  4.     age = NONE  

  5.     name(Name) =   

  6.         {  

  7.             last = NONE  

  8.             first = NONE  

  9.         }  

  10.    }  

 #设置变量

Java代码  收藏代码
  1. phone = client.factory.create('Phone')  

  2. phone.npa = 202  

  3. phone.nxx = 555  

  4. phone.number = 1212  

 

Python代码  收藏代码
  1. name = client.factory.create('Name')  

  2. name.first = 'Elmer'  

  3. name.last = 'Fudd'  

 

Python代码  收藏代码
  1. person.name = name  

  2. person.age = 35  

  3. person.phone = [phone]  

  4.   

  5. #或者  

  6. person.phone.append(phone)  

 

Java代码  收藏代码
  1. try:  

  2.    person_added = client.service.addPerson(person)  

  3. except WebFault, e:  

  4.   print e  

 

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

 


Python代码  收藏代码
  1. person = {}  

  2. #根据对象结构构造json  

  3.   

  4. phone = {  

  5.     'npa':202,  

  6.     'nxx':555,  

  7.     'number':1212,  

  8. }  

  9.   

  10. name = {  

  11.     'first':'Elmer',  

  12.     'last':'Fudd'  

  13. }  

  14.   

  15. person['name'] = name  

  16. person['age'] = 35  

  17. person['phone'] = [phone,]  

  18.   

  19. try:  

  20.    person_added = client.service.addPerson(person)  

  21. except WebFault, e:  

  22.   print e  

 3.异常处理


Python代码  收藏代码
  1. client = client(url, faults=False)  

  2. result = client.service.addPerson(person)  

  3. print result  

  4. 200, person ...)  

更多可以查看官方文档:https://fedorahosted.org/suds/wiki/Documentation,里面还讲了soap头得安全认证,webservice cache等高级话题,有需要可以查看,都比较详细。


python3 请使用

pip3 install suds-jurko



转自:https://ully.iteye.com/blog/1266320

群贤毕至

访客