Web Service系列之实例之spyne

来源:互联网 发布:穷女生恋爱知乎 编辑:程序博客网 时间:2024/06/05 05:57

原文链接: Web Service系列之实例之spyne

概要

本文主要关注利用spyne实现web service, 接口参数为自定义数据类型数组

开发环境

➜  ~ cat /etc/redhat-release Fedora release 25 (Twenty Five)
➜  ~ python3Python 3.5.2 (default, Sep 14 2016, 11:28:32) >>> import spyne, suds, zeep>>> spyne.__version__'2.12.14'>>> suds.__version__, zeep.__version__('1.3.3.0', '1.4.1')

spyne ( https://github.com/arskom/spyne )

spyne webservice endpoint

接口说明
* say_hello_plus, 参数为Array(Name), 其中Name为自定义类型
* say_hello_plus2, 参数为Array(String)

完整代码

from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode,\ComplexModel, Array, Stringfrom spyne.protocol.soap import Soap11from spyne.server.wsgi import WsgiApplicationclass Name(ComplexModel):    first_name = Unicode    last_name = Unicodeclass HelloWorldService(ServiceBase):    @rpc(Unicode, Integer, _returns=Iterable(Unicode))    def say_hello(ctx, 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 times the number of times to say hello        @return the completed array        """        for i in range(times):            yield u'Hello, %s' % name    @rpc(Array(Name), _returns=Iterable(Unicode))    def say_hello_plus(self, name_plus):        print('---', name_plus)        if not name_plus:            yield 'None'        for name in name_plus:            print(name.first_name)            print(name.last_name)            yield name.first_name+name.last_nameclass HelloWorldService2(ServiceBase):    @rpc(Array(String), _returns=Iterable(Unicode))    def say_hello_plus2(self, name_plus):        if not name_plus:            yield 'None'        for name in name_plus:            yield nameapplication = Application([HelloWorldService, HelloWorldService2], 'spyne.examples.hello.soap',                          in_protocol=Soap11(validator='lxml'),                          out_protocol=Soap11())wsgi_application = WsgiApplication(application)if __name__ == '__main__':    import logging    from wsgiref.simple_server import make_server    logging.basicConfig(level=logging.DEBUG)    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)    logging.info("listening to http://127.0.0.1:8000")    logging.info("wsdl is at: http://localhost:8000/?wsdl")    server = make_server('127.0.0.1', 8000, wsgi_application)    server.serve_forever()

suds - Python client

from suds.client import Clientip = '127.0.0.1'port = 8000client = Client("http://%s:%s/?wsdl" % (ip, port))#print(client)### say_hello_plusname = {}name["first_name"] = "Kylin"name["last_name"] = "Shu"names = client.factory.create("NameArray")names.Name.append(name)names.Name.append(name)r = client.service.say_hello_plus(names)print(r)### say_hello_plus2names = client.factory.create("stringArray")name = "Kylin"names.string.append(name)names.string.append(name)#r = client.service.say_hello('AAA', 2)#print(r)r = client.service.say_hello_plus2(names)print(r)

运行结果

➜  test_ws python3 ./client_spyne_suds.py(stringArray){   string[] =       "KylinShu",      "KylinShu", }(stringArray){   string[] =       "Kylin",      "Kylin", }

zeep - Python client

from zeep import Clientip = '127.0.0.1'port = 8000client = Client("http://%s:%s/?wsdl" % (ip, port))#print(client.wsdl.dump())### say_hello_plusfactory = client.type_factory("ns0")name = factory.Name(first_name='Kylin', last_name='Shu')names = factory.NameArray([name, name])r = client.service.say_hello_plus(names)print(r)### say_hello_plusfactory = client.type_factory("ns0")names = factory.stringArray(["Kylin", "Shu"])r = client.service.say_hello_plus2(names)print(r)

运行结果

➜  test_ws python3 ./client_spyne_zeep.py ['KylinShu', 'KylinShu']['Kylin', 'Shu']
0 0
原创粉丝点击