用soaplib 创建 WebService

前端之家收集整理的这篇文章主要介绍了用soaplib 创建 WebService前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

soaplib 组件下载

import soaplib

from soaplib.service import rpc
from soaplib.service import DefinitionBase
from soaplib.model.primitive import String,Integer

from soaplib.server import wsgi
from soaplib.model.clazz import Array

'''
This is a simple HelloWorld example to show the basics of writing
a webservice using soaplib,starting a server,and creating a service
client.
'''

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 results

if __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"
~                                                                                            

猜你在找的WebService相关文章