Table of contents
Based on Python3 and spyne toolkit, develop WebService server
1. WebService server based on spyne
1.1. Basic environment
Use Python (3.7), spyne (2.13.15), wsgiref (included)
pip install spyne
1.1. Server source code
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.
<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'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.
<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
"""
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.
<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'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.
<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
"""
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()
The above code can be used directly, and the following is a WebService client to access.
2. Client access test
Here use suds as a client for testing.
For details about suds, please refer to the first section of "Developing WebService-2 Based on Python: Client (suds, zeep)"
2.1. Basic environment
Python3.7、suds_jurko(0.6)
pip install suds_jurko
2.2. Client source code
The return value of the method written by the server is stringArray, which can be accessed like a 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. Test
- Run the server first, if there is no problem, you will see the following results
- You can use the edge browser to log in to this URL to view
- Run the client code, you can see the following content in the console.
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