WebService 开发
WebService 是一种跨编程语言和跨操作系统平台的远程调用技术。
理解 WebService
-
从表面上看,WebService 就是一个应用程序向外界暴露出一个能通过 Web 进行调用的 API,也就是说能用编程的方法通过 Web 来调用这个应用程序。我们把调用这个 WebService 的应用程序叫做客户端,而把提供这个 WebService 的应用程序叫做服务端。
-
从深层次看,WebService 是建立可互操作的分布式应用程序的新平台,是一个平台,是一套标准。它定义了应用程序如何在 Web 上实现互操作性,你可以用任何你喜欢的语言,在任何你喜欢的平台上写 Web service ,只要我们可以通过 Web service 标准对这些服务进行查询和访问。
Python 库选择
服务端开发:
针对 Python 的 WebService 开发,开发者讨论最多的库是 soaplib(官方地址:http://soaplib.github.io/soaplib/2_0/index.html),但从其官网可知,其最新版本 “soaplib-2.0.0-beta2” 从 2011 年 3 月发布后就不再进行更新了。通过阅读 soaplib 的官方文档,可知其不再维护后已经转向了一个新的项目:rpclib(官方地址:http://github.com/arskom/rpclib)进行后续开发,但在 rpclib 的 readme 中,介绍了 rpclib 已经更名为 spyne,并将持续进行更新,so,那就选用 spyne 进行开发了。
spyne 官方文档:http://spyne.io/docs/2.10/index.html
spyne github:https://github.com/arskom/spyne
-
spyne 安装:
pip install spyne
-
lxml 安装:
下载与 python 匹配的版本安装包 https://pypi.python.org/pypi/lxml/3.6.0 进行安装,如
客户端开发:
客户端调用 WebService 一般应用 suds 库。
使用参考文档:https://fedorahosted.org/suds/wiki/Documentation
-
suds 安装:
pip install suds
Spyne Introduction
Protocols:协议 Protocols define the rules for transmission of structured data Transports: 传输 Transports, also protocols themselves, encapsulate protocol data in their free-form data sections. Models: 模式 Types like Unicode, Integer or ByteArray are all models. They reside in the spyne.model package. Interface Documents: 接口文档 Interface documents provide a machine-readable description of the expected input and output of the exposed method calls. Serializers: 序列化对象 Serializers are currently not distinguished in Spyne code. They are the protocol-specific representations of a serialized Python object.
How your code is wrapped?
step1:Your code is inside @rpc-wrapped methods in ServiceBase subclasses.
step2:The ServiceBase subclasses in turn are wrapped by an Application instance.
The Application instantiation is used to assign input and output protocols to the exposed methods.
step3:The Application instance is finally wrapped by a client or server transport that takes the responsibility of moving the bits around.
step4:Deploying the service using Soap via Wsgi
服务端代码实例(HelloWorld)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
preference:
http://spyne.io/docs/2.10/index.html
https://github.com/arskom/spyne/blob/master/examples/helloworld_soap.py
This is a simple HelloWorld example to show the basics of writing
a webservice using spyne, starting a server, and creating a service
client.
Here's how to call it using suds:
#>>> from suds.client import Client
#>>> hello_client = Client('http://localhost:8000/?wsdl')
#>>> hello_client.service.say_hello('punk', 5)
(stringArray){
string[] =
"Hello, punk",
"Hello, punk",
"Hello, punk",
"Hello, punk",
"Hello, punk",
}
#>>>
"""
# Application is the glue between one or more service definitions, interface and protocol choices.
from spyne import Application
# @rpc decorator exposes methods as remote procedure calls
# and declares the data types it accepts and returns
from spyne import rpc
# spyne.service.ServiceBase is the base class for all service definitions.
from spyne import ServiceBase
# The names of the needed types for implementing this service should be self-explanatory.
from spyne import Iterable, Integer, Unicode
from spyne.protocol.soap import Soap11
# Our server is going to use HTTP as transport, It’s going to wrap the Application instance.
from spyne.server.wsgi import WsgiApplication
# step1: Defining a Spyne Service
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(self, 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 When returning an iterable, you can use any type of python iterable. Here, we chose to use generators.
"""
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', 8000, wsgi_app)
server.serve_forever()
服务端运行后,
访问浏览器检查服务 http://localhost:8000/?wsdl
浏览器中输出 wsdl 文件:
客户端调用(代码实例)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from suds.client import Client # 导入suds.client 模块下的Client类
wsdl_url = "http://localhost:8000/?wsdl"
def say_hello_test(url, name, times):
client = Client(url) # 创建一个webservice接口对象
client.service.say_hello(name, times) # 调用这个接口下的getMobileCodeInfo方法,并传入参数
req = str(client.last_sent()) # 保存请求报文,因为返回的是一个实例,所以要转换成str
response = str(client.last_received()) # 保存返回报文,返回的也是一个实例
print req # 打印请求报文
print response # 打印返回报文
if __name__ == '__main__':
say_hello_test(wsdl_url, 'Milton', 2)
客户端运行后,
查看客户端控制台可见输出:
本文来自博客园,作者:游走De提莫,转载请注明原文链接:https://www.cnblogs.com/Gaimo/p/16098005.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律