基于Python开发WebService-1:服务端(spyne)

目录

  • 1、基于spyne的WebService服务端
    • 1.1、基础环境
    • 1.1、服务端源码
  • 2、客户端访问测试
    • 2.1、基础环境
    • 2.2、客户端源码
    • 2.3、测试

基于Python3、spyne工具包,开发WebService服务端

1、基于spyne的WebService服务端

1.1、基础环境

使用Python(3.7)、spyne(2.13.15)、wsgiref(自带)

pip install spyne

1.1、服务端源码

from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode, String
# 如果支持soap的协议需要用到Soap11
from spyne.protocol.soap import Soap11
# 可以创建一个wsgi服务器,做测试用
from spyne.server.wsgi import WsgiApplication


class HelloWorldService1(ServiceBase):
	# 输入和输出的类型,这里返回值是stringArray
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello1(ctx, name, times):
        """Docstrings for service methods appear as documentation in the wsdl.
        What fun!
        @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'say_hello1 : Hello, %s' % name

    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def test1(ctx, name, times):
        """Docstrings for service methods appear as documentation in the wsdl.
        What fun!
        @param name the name to say hello to
        @param times the number of times to say hello
        @return the completed array
        """
        return  [u'test1 : hello, %s - %d' % (name, i) for i in range(times)]

class HelloWorldService2(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello2(ctx, name, times):
        """Docstrings for service methods appear as documentation in the wsdl.
        What fun!
        @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'say_hello2 : Hello, %s' % name

    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def test2(ctx, name, times):
        """Docstrings for service methods appear as documentation in the wsdl.
        What fun!
        @param name the name to say hello to
        @param times the number of times to say hello
        @return the completed array
        """
        return  [u'test2 : hello, %s - %d' % (name, i) for i in range(times)]


application = Application([HelloWorldService1, HelloWorldService2], 'http://schemas.xmlsoap.org/soap/envelope',
                          in_protocol=Soap11(validator='lxml'), out_protocol=Soap11())
wsgi_application = WsgiApplication(application)

if __name__ == '__main__':
    import logging
    # wsgiref是python内置的一个简单的、遵循wsgi接口的服务器。
    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:8080")
    logging.info("wsdl is at: http://127.0.0.1:8080/?wsdl")

	# 127.0.0.1改成你的IP,让客户端所在电脑能访问就行
	server = make_server('127.0.0.1', 8080, wsgi_application)
    server.serve_forever()

以上的代码可以直接用,下面做一个WebService客户端去访问。

2、客户端访问测试

这里用suds做一个客户端进行测试。
关于suds的详细内容可以查看《基于Python开发WebService-2:客户端(suds、zeep)》中第一节的内容

2.1、基础环境

Python3.7、suds_jurko(0.6)

pip install suds_jurko

2.2、客户端源码

服务端写的method的返回值是stringArray,可以像list一样去访问。

# 基于suds_jurko做webservice客户端
from suds.client import Client

if __name__ == '__main__':
    url = 'http://127.0.0.1:8080/?wsdl'
    client = Client(url)
    print(client)
    
    print('-'*20)
    
    print(client.service.say_hello1('张三', 5))
    print(client.service.say_hello2('李四', 5))
    print(client.service.test1('测试1', 3))
    a = client.service.test2('测试2', 3)
    print(a)
    print(a[0])
    print(a[0][0])

2.3、测试

  1. 先运行服务端,无问题的话,会看到以下结果基于Python开发WebService-1:服务端(spyne)_第1张图片
  2. 可以用edge浏览器登录这个网址去查看基于Python开发WebService-1:服务端(spyne)_第2张图片
  3. 运行客户端的代码,可以再控制台看到以下内容。
Suds ( https://fedorahosted.org/suds/ )  version: 0.6

Service ( HelloWorldService1 ) tns="http://schemas.xmlsoap.org/soap/envelope"
   Prefixes (1)
      ns0 = "http://schemas.xmlsoap.org/soap/envelope"
   Ports (1):
      (Application)
         Methods (4):
            say_hello1(xs:string name, xs:integer times)
            say_hello2(xs:string name, xs:integer times)
            test1(xs:string name, xs:integer times)
            test2(xs:string name, xs:integer times)
         Types (9):
            say_hello1
            say_hello1Response
            say_hello2
            say_hello2Response
            stringArray
            test1
            test1Response
            test2
            test2Response



Service ( HelloWorldService2 ) tns="http://schemas.xmlsoap.org/soap/envelope"
   Prefixes (1)
      ns1 = "http://schemas.xmlsoap.org/soap/envelope"
   Ports (1):
      (Application)
         Methods (4):
            say_hello1(xs:string name, xs:integer times)
            say_hello2(xs:string name, xs:integer times)
            test1(xs:string name, xs:integer times)
            test2(xs:string name, xs:integer times)
         Types (9):
            say_hello1
            say_hello1Response
            say_hello2
            say_hello2Response
            stringArray
            test1
            test1Response
            test2
            test2Response


--------------------
(stringArray){
   string[] =
      "say_hello1 : Hello, 张三",
      "say_hello1 : Hello, 张三",
      "say_hello1 : Hello, 张三",
      "say_hello1 : Hello, 张三",
      "say_hello1 : Hello, 张三",
 }
(stringArray){
   string[] =
      "say_hello2 : Hello, 李四",
      "say_hello2 : Hello, 李四",
      "say_hello2 : Hello, 李四",
      "say_hello2 : Hello, 李四",
      "say_hello2 : Hello, 李四",
 }
(stringArray){
   string[] =
      "test1 : hello, 测试1 - 0",
      "test1 : hello, 测试1 - 1",
      "test1 : hello, 测试1 - 2",
 }
(stringArray){
   string[] =
      "test2 : hello, 测试2 - 0",
      "test2 : hello, 测试2 - 1",
      "test2 : hello, 测试2 - 2",
 }
[test2 : hello, 测试2 - 0, test2 : hello, 测试2 - 1, test2 : hello, 测试2 - 2]
test2 : hello, 测试2 - 0

你可能感兴趣的:(WebService,Python笔记)