close

web Service是一种跨编程语言和跨操作系统平台的远程调用技术。使原来各孤立的站点之间的信息能够相互通信、

共享而提出的。简单点就是 基于web的远程调用.

 

http://www.binqsoft.com/blog/2017/08/13/__trashed-3/

需要安裝下列安裝包

pip install spyne

pip install suds-jurko

==============================

參考影片 :  https://www.youtube.com/watch?v=phddHbrK4T8&feature=youtu.be

 

==============Server============

# -*- coding: utf-8 -*-
from spyne.application import Application
# 加了rpc装饰器后,该方法将暴露给客户端调用者,同时声明了接受的参数以及返回结果的数据类型
from spyne.decorator import srpc
# spyne.service.ServiceBase是所有定义服务的基类
from spyne.service import ServiceBase
from spyne.model.complex import Iterable
from spyne.model.primitive import UnsignedInteger
from spyne.model.primitive import String
from spyne.protocol.soap import Soap11
# 服务采用http来传输, 包装成一个WsgiApplication实例应用
from spyne.server.wsgi import WsgiApplication
# step1: 定义一个Spyne服务
class HelloWorldService(ServiceBase):


    @srpc(String, UnsignedInteger, _returns=Iterable(String))
    def say_hello(name, times):
        """
        @param name:
        @param times: the number of times to say hello
        @return  返回一个迭代器
        """
        for i in range(times):
            yield u'Hello, %s' % name
# step2: Glue the service definition, input and output protocols
soap_app = Application([HelloWorldService], 'spyne.examples.hello.soap',
                       in_protocol=Soap11(validator='lxml'),
                       out_protocol=Soap11())
# step3: Wrap the Spyne application with its wsgi wrapper
wsgi_app = WsgiApplication(soap_app)
if __name__ == '__main__':
    import logging
    from wsgiref.simple_server import make_server
    # configure the python logger to show debugging output
    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")
    # step4:Deploying the service using Soap via Wsgi
    # register the WSGI application as the handler to the wsgi server, and run the http server
    server = make_server('127.0.0.1', 8001, wsgi_app)
    server.serve_forever()

 

==========Client=========================

from suds.client import Client
hello_client = Client('http://localhost:8001/?wsdl')
print (hello_client.service.say_hello("Dave", 5))

 

 

 

 

 

 

 

 

 

===============其他參考=============

https://www.cnblogs.com/guanfuchang/p/5985070.html

 

https://www.cnblogs.com/guanfuchang/p/5985070.html

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ricky10116r2d2 的頭像
    ricky10116r2d2

    ricky10116r2d2的部落格

    ricky10116r2d2 發表在 痞客邦 留言(0) 人氣()