zoukankan      html  css  js  c++  java
  • Python开发WebService使用soaplib库

    使用soaplib开发基于Python语言的WebService主要有以下四个步骤:

    一、准备环境

        S1:下载插件Python、soaplib、Twisted、lxml(soaplib依赖于lxml这个库)

        S2:使用easy_install快速安装,如进入Python安装目录下(该目录必须已经添加到环境变量),输入命令:easy_install soaplib,easy_install这个工具会自动  到Internet上帮我们寻找最适合本机安装的版本并自动安装,安装完毕即可。

        S3:我的环境配置:Python 2.7 、Twisted-12.0.0-py2.7、soaplib-2.0.0_beta2-py2.7、lxml-2.3-py2.7-

    二、开发WebService

        S1:使用Python开发工具,这里推荐两个Python开发工具(1:JetBrains Pycharm;2:Eclipse),在新建的Py文件中导入以下命名空间:

               import soaplib

        from soaplib.core.util.wsgi_wrapper import run_twisted #(用来启动服务,soaplib1.0不是这样引用的,这是2.0的引用方式)
        from soaplib.core.server import wsgi
        from soaplib.core.service import DefinitionBase #(所有的服务类都继承DefinitionBase基类)
        from soaplib.core.service import soap #(soap标识方法的特性)
        from soaplib.core.model.clazz import Array #(Array是返回集合类型的数据)
        from soaplib.core.model.binary import Attachment
        from soaplib.core.model.clazz import ClassModel #(如果返回的是实体类的话,必须继承ClassModel基类)
        from soaplib.core.model.primitive import Integer,String,Boolean #(Integer是返回整形,String是返回字符串类型,Boolean是返回布尔类型)

      S2:下面是一个简单的例子:

        class HelloWorldService(DefinitionBase):
          @soap(String, _returns=String) #(标识方法的实参以及返回值,输入参数可以定义多个,该方法要求输入字符串,返回字符串
          def say_hello(self, name):#(如果有传入参数的话,按照顺序写)
            return "Hello %s!" % name#(返回值)

          @soap(_returns=Array(String))#(该方法没有输入参数的定义,返回字符串类型的集合,其中Array中必须写返回的具体数据类型
          def GetCdrArray(self):
            L_Result=["1","2","3"]#(返回集合数据的格式
            return L_Result

          @soap(_returns=C_ProbeCdrModel)#(该方法没有输入参数的定义,返回实体类C_ProbeCdrModel类型,类的定义详见最后

          def GetCdr(self):

            L_Obj=C_ProbeCdr()
            L_Model=C_ProbeCdrModel()
            L_Model.Name=L_Result.Name
            L_Model.Id=L_Result.Id
            return L_Result

        

        class C_ProbeCdrModel(ClassModel):#(返回的是实体类的,必须继承ClassModel基类

          __namespace__ = "C_ProbeCdrModel"
          Name=String#(定义属性的数据类型为String)不能省略数据类型
          Id=Integer#(定义属性的数据类型为Integer)不能省略数据类型

    三、发布Webservice

      if __name__=="__main__":

        soap_app=soaplib.core.Application([HelloWorldService], 'tns')#(中括号伪服务类的名称)
        wsgi_app=wsgi.Application(soap_app)
        print 'listening on 127.0.0.1:7789'
        print 'wsdl is at: http://127.0.0.1:7789/SOAP/?wsdl'
        run_twisted( ( (wsgi_app, "SOAP"),), 7789)#(运行twisted服务器加载该服务)

    四、访问Python发布的WebService

      S1:在浏览器重访问http://127.0.0.1:7789/SOAP/?wsdl,如果正常的话,则能看到该服务的描述信息,包括各个方法的输入参数、返回值,以及实体类的信息(如果用到了的话)

      S2:此时使用你自己的开发语言通过添加服务引用进行服务接口的调用就可以了。

    以上若有纰漏之处,还请不吝点拨,共同提高,非常感谢!

  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/grok/p/2476177.html
Copyright © 2011-2022 走看看