Web Service 之 Python -- spyne
概要
本文主要關注利用spyne實現web service, 介面引數為自定義資料型別和陣列
pip install spyne, suds, zeep
>>> 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 )
Server部分
#!/usr/bin/python # coding:utf-8 from spyne import Application, rpc, ServiceBase from spyne import Integer, Unicode, Array, ComplexModel, Iterable, String from spyne.protocol.soap import Soap11 from spyne.server.wsgi import WsgiApplication from wsgiref.simple_server import make_server class Person(ComplexModel): name = Unicode age = Integer class HelloWorldService(ServiceBase): @rpc(Unicode, Integer, _returns=Iterable(Unicode)) def say_hello(self, name, times): for i in range(times): yield "Hello %s, It's the %s time to meet you." % (name, i + 1) @rpc(Array(Person), _returns=Iterable(Unicode)) def say_hello_1(self, persons): print('-------say_hello_1()--------') if not persons: yield 'None' for person in persons: print('name is : %s, age is %s.' % (person.name, person.age)) yield 'name is : %s, age is %s.' % (person.name, person.age) class HelloWorldService2(ServiceBase): @rpc(Array(String), _returns=Iterable(Unicode)) def say_hello_2(self, persons): if not persons: yield 'None' for person in persons: yield person application = Application([HelloWorldService, HelloWorldService2], 'spyne.examples.hello', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11()) wsgi_application = WsgiApplication(application) if __name__ == '__main__': import logging host = '127.0.0.1' port = 8000 logging.info("listening to http://127.0.0.1:8000") logging.info("wsdl is at: http://localhost:8000/?wsdl") server = make_server(host, port, wsgi_application) server.serve_forever()
Client部分(Suds)
#!/usr/bin/python # coding:utf-8 from suds.client import Client host = '127.0.0.1' port = 8000 client = Client('http://%s:%s/?wsdl' % (host, port)) # print client persons = client.service.say_hello('zhangsan', 2) print persons print '-' * 20 person = {} person['name'] = 'zhangsan' person['age'] = 23 persons = client.factory.create('PersonArray') persons.Person.append(person) persons.Person.append(person) person = client.service.say_hello_1(persons) print person print '=' * 20 persons = client.factory.create('stringArray') persons.string.append('lisi') persons.string.append('zhangsan') person = client.service.say_hello_2(persons) print person
zeep - Python client
#!/usr/bin/python # coding:utf-8 from zeep import Client ip = '127.0.0.1' port = 8000 client = Client("http://%s:%s/?wsdl" % (ip, port)) # print(client.wsdl.dump()) ### say_hello factory = client.type_factory("ns0") r = client.service.say_hello('zhansgan', 3) print(r) ### say_hello_1 factory = client.type_factory("ns0") person = factory.Person(name='zhangsan', age=23) persons = factory.PersonArray([person, person]) r = client.service.say_hello_1(persons) print(r) ### say_hello_2 factory = client.type_factory("ns0") persons = factory.stringArray(["zhansgan", "lisi"]) r = client.service.say_hello_2(persons) print(r)
相關文章
- Spring Web Service教程SpringWeb
- Python之Web框架DjangoPythonWeb框架Django
- 教你入門Web ServiceWeb
- Android中的Web ServiceAndroidWeb
- Python主流Web框架之TornadoPythonWeb框架
- Java:Web Service初入門JavaWeb
- Kubernetes Service之ClusterIP
- 如何使用ABSL程式碼呼叫Web serviceWeb
- k8s之Service詳解-Service使用K8S
- Android 之Service使用攻略Android
- vivo web service:億萬級規模web服務引擎架構Web架構
- More web function requests go online concurrently, and web service deployment is faster and more economical!WebFunctionGoAST
- Service Worker (Web)推送不完全實踐Web
- 【Azure Bot Service】部署Python ChatBot程式碼到App Service中PythonAPP
- Service Mesh之Istio部署bookinfo
- SAP CRM如何建立支援Web Service的WORD模板Web
- SAP CRM如何建立支援Web Service的PDF模板Web
- 使用soapUI消費SAP Cloud for Customer的web serviceUICloudWeb
- Web service,XSD及WSDL的一些使用Web
- 如何使用FastReport.Service.dll建立Web服務ASTWeb
- python webPythonWeb
- 基於Python的web架構之Tornado+FlaskPythonWeb架構Flask
- Python入門之web2py框架學習!PythonWeb框架
- 【Azure 應用服務】Azure App Service For Linux 上實現 Python Flask Web Socket 專案 Http/HttpsAPPLinuxPythonFlaskWebHTTP
- 使用nodejs消費SAP Cloud for Customer上的Web serviceNodeJSCloudWeb
- 如何在SAP CRM裡建立和消費Web serviceWeb
- [譯] Service workers:Progressive Web Apps 背後的小英雄WebAPP
- python web 部署PythonWeb
- Python Web DevPythonWebdev
- Android 四大元件之 ServiceAndroid元件
- swarm mode叢集之service分組Swarm
- 微服務框架之微軟Service Fabric微服務框架微軟
- Android之Service設定android:process作用Android
- 服務網格service mesh 之 Linkerd
- Service Mesh之Istio基礎入門
- k8s重器之ServiceK8S
- Difference between Microsoft Dynamics 365 WEB API, Organization Service and Organization Data ServicROSWebAPI
- 四大元件之Service_AIDL元件AI
- Android四大元件之Service篇Android元件