python开发webservice例子

jopen 5年前

服务器段代码:

import soaplib      from soaplib.core.server import wsgi  from soaplib.core.service import DefinitionBase  from soaplib.core.service import soap  from soaplib.core.model.clazz import Array  from soaplib.core.model.clazz import ClassModel  from soaplib.core.model.primitive import Integer,String  class C_ProbeCdrModel(ClassModel):          __namespace__ = "C_ProbeCdrModel"          Name=String          Id=Integer  class AdditionService(DefinitionBase):  #this is a web service          @soap(Integer,Integer,_returns=String)          def addition(self,a,b):                  print 'aaaaaaaaaaaa'                  return str(a)+'+'+str(b)+'='+str(a+b)          @soap(_returns=Array(String))          def GetCdrArray(self):                  print 'bbbbbbbbbb'                  L_Result=["1","2","3"]                  return L_Result          @soap(_returns=C_ProbeCdrModel)          def GetCdr(self):                  print 'ccccccc'                  L_Model=C_ProbeCdrModel()                  L_Model.Name=L_Model.Name                  L_Model.Id=L_Model.Id                  return L_Model          if __name__=='__main__':          try:                  print 'service start'                  from wsgiref.simple_server import make_server                  soap_application = soaplib.core.Application([AdditionService], 'tns')                  wsgi_application = wsgi.Application(soap_application)                  server = make_server('localhost', 7789, wsgi_application)                  server.serve_forever()              except ImportError:                  print 'error'

 

客户端代码:

__author__ = 'admin'  from suds.client import Client  test=Client('http://localhost:7789/SOAP/?wsdl')  print test