用soaplib 创建 WebService

来源:互联网 发布:怎么查看端口是否打开 编辑:程序博客网 时间:2024/05/17 16:55

经过多次实验失败的总结,发现官方给的soaplib或是yum, pip ,easy_install 安装的都会存在问题(Python2.7)。所以把这个记下来以防不时之需。一定要从这里下载,才能保证源码可运行。

soaplib 组件下载

import soaplibfrom soaplib.service import rpcfrom soaplib.service import DefinitionBasefrom soaplib.model.primitive import String, Integerfrom soaplib.server import wsgifrom soaplib.model.clazz import Array'''This is a simple HelloWorld example to show the basics of writinga webservice using soaplib, starting a server, and creating a serviceclient.'''class HelloWorldService(DefinitionBase):    @rpc(String, Integer, _returns=Array(String))    def say_hello(self, name, times):        '''        Docstrings for service methods appear as documentation in the wsdl        <b>what fun</b>        @param name the name to say hello to        @param the number of times to say hello        @return the completed array        '''        results = []        for i in range(0, times):            results.append('Hello, %s' % name)        return resultsif __name__=='__main__':    try:        from wsgiref.simple_server import make_server        soap_application = soaplib.Application([HelloWorldService], 'tns')        wsgi_application = wsgi.Application(soap_application)        print "listening to http://0.0.0.0:80"        print "wsdl is at: http://127.0.0.1:80/?wsdl"        server = make_server('223.223.83.238',80, wsgi_application)        server.serve_forever()    except ImportError:        print "Error: example server code requires Python >= 2.5"~                                                                                            


原创粉丝点击