Python 开发与测试 Webservice(SOAP)
Python 开发与测试 Webservice(SOAP)
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
理解WebService
1.从表面上看,WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程的方法通过Web来调用这个应用程序。我们把调用这个WebService的应用程序叫做客户端,而把提供这个WebService的应用程序叫做服务端。
2.从深层次看,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 安装:
1 |
|
- lxml 安装:
下载与python匹配的版本安装包 https://pypi.python.org/pypi/lxml/3.6.0 进行安装,如 lxml-3.6.0.win-amd64-py2.7.exe (md5)
客户端开发:
客户端调用WebService一般应用suds库。
使用参考文档:https://fedorahosted.org/suds/wiki/Documentation
- suds 安装:
1 |
|
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)
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
服务端运行后,
访问浏览器检查服务 http://localhost:8000/?wsdl
浏览器中输出wsdl文件:
客户端调用(代码实例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
客户端运行后,
查看客户端控制台可见输出: