原文链接: http://www.kylin-ux.com/2017/04/21/network-web-web-service系列之实例之spyne

概要

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

开发环境

1
2
➜ ~ cat /etc/redhat-release
Fedora release 25 (Twenty Five)
1
2
3
4
5
6
7
➜ ~ python3
Python 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)

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode,\
ComplexModel, Array, String
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
class Name(ComplexModel):
first_name = Unicode
last_name = Unicode
class 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_name
class 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 name
application = 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from suds.client import Client
ip = '127.0.0.1'
port = 8000
client = Client("http://%s:%s/?wsdl" % (ip, port))
#print(client)
### say_hello_plus
name = {}
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_plus2
names = 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)

运行结果

1
2
3
4
5
6
7
8
9
10
11
➜ test_ws python3 ./client_spyne_suds.py
(stringArray){
string[] =
"KylinShu",
"KylinShu",
}
(stringArray){
string[] =
"Kylin",
"Kylin",
}

zeep - Python client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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_plus
factory = 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_plus
factory = client.type_factory("ns0")
names = factory.stringArray(["Kylin", "Shu"])
r = client.service.say_hello_plus2(names)
print(r)

运行结果

1
2
3
➜ test_ws python3 ./client_spyne_zeep.py
['KylinShu', 'KylinShu']
['Kylin', 'Shu']