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
- Web Service系列之实例之spyne
- Web Service系列之实例之JAX-WS
- Web Service系列之常见问题
- Web Service系列之实例之使用urllib发送SOAP POST请求
- Web Service系列之实例之使用http.client发送SOAP POST请求
- Web Service系列之数组参数
- 构建安全的Xml Web Service系列之SSL篇
- 入学习Web Service系列之异步开发模式
- 构建安全的Xml Web Service系列之SSL篇
- 构建安全的Xml Web Service系列之SSL篇
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI
- Web Service单元测试工具实例介绍之SoapUI .
- Web Service单元测试工具实例介绍之SoapUI
- hdu 3584 Cube(三维树状数组)
- requests+beautifulsoup4 爬虫实战
- 【JVM】JVM系列之垃圾回收(二)
- 内部类
- 硅谷未来科技与创业企业
- Web Service系列之实例之spyne
- 机器学习的最佳入门学习资源
- python犯错记录(长期更新)
- java集合框架(二)——Collection(集合)类
- 【面试】基于二叉树层次遍历相关问题的求解
- AOP的正确打开方式
- easyui使用总结(二)
- SpringMVC学习(1):SpringMVC框架结构详解和入门案例详解
- bzoj 4864: [BeiJing 2017 Wc]神秘物质 splay