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 安装:
pip install spyne
- 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 安装:
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.
<b>What fun!</b>
@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)
客户端运行后,
查看客户端控制台可见输出:
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
Python 开发与测试 Webservice(SOAP)的更多相关文章
- python实现建立soap通信(调用及测试webservice接口)
实现代码如下: #调用及测试webservice接口 import requests class SoapConnect: def get_soap(self,url,data): r = reque ...
- 使用python开发WebService
使用python开发WebService 分类: web linux2009-03-30 11:36 6621人阅读 评论(1) 收藏 举报 webservicepythonsoapimportecl ...
- 关于python测试webservice接口的视频分享
现在大公司非常流行用python做产品的测试框架,还有对于一些快速原型产品的开发也好,很好地支持OO编程,代码易读.Python的更新挺快的,尤其是第三方库. 对于测试人员,代码基础薄弱,用pytho ...
- python 开发webService
最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...
- python开发笔记-python调用webservice接口
环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...
- Python开发WebService:REST,web.py,eurasia,Django
Python开发WebService:REST,web.py,eurasia,Django 博客分类: Python PythonRESTWebWebServiceDjango 对于今天的WebSe ...
- 《Python Web 接口开发与测试》---即将出版
为什么要出这样一本书? 首先,今年我有不少工作是跟接口自动化相关的,工作中的接口自动化颇有成效. 我一直是一个没有测试大格局的人,在各种移动测试技术爆发的这一年,我却默默耕耘着自己的一亩三分地儿(We ...
- python的高性能web应用的开发与测试实验
python的高性能web应用的开发与测试实验 tornado“同步和异步”网络IO模型实验 引言 python语言一直以开发效率高著称,被广泛地应用于自动化领域: 测试自动化 运维自动化 构建发布自 ...
- 老司机带你用vagrant打造一站式python开发测试环境
前言 作为一个学习和使用Python的老司机,好像应该经常总结一点东西的,让新司机尽快上路,少走弯路,然后大家一起愉快的玩耍. 今天,咱们就使用vagrant配合xshell打造一站式Python ...
随机推荐
- 哈希 poj 3274
n个牛 二进制最多k位 给你n个数 求max(j-i)&&对应二进制位的和相同 7 1 1 1 倒的 6 0 1 1 7 1 1 1 2 0 1 ...
- 51nod 1060反素数
经典题. #include<map> #include<queue> #include<stack> #include<cmath> #include& ...
- C#-黑客-数据库访问-字符串的攻击和防御
C#中用基本的方法对数据库进行增删改查,会被黑客利用,写入其他的代码以实现对数据库的数据进行其他的操作.例如: 对下列数据库的某个信息进行修改操作 修改代码: using System; using ...
- 【HDU 5835】Danganronpa(分配礼物)
10种礼物,每种有ai个,每个小朋友分两个礼物,其中普通礼物要求相邻两人的不能一样,求最多分给几个小朋友. sum/2是最多的情况.什么时候发不了那么多,就是当max很大,无论怎么发,都发不完max. ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- 【BZOJ-3165】Segment 李超线段树(标记永久化)
3165: [Heoi2013]Segment Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 368 Solved: 148[Submit][Sta ...
- C#调试方法
单步执行 有三种, 一种是每次执行一行(F10): 一种是每次执行一行,但遇到函数调用就会跳到被调用的函数里(F11): 一种是直接执行当前函数里剩下的指令,返回上一级函数(Shift+F11). 还 ...
- [IOS NSUserDefaults]的使用:登陆后不再显示登录界面。
之前搜了好多地方都没找到实现“登陆后不再显示登录界面,而默认自动登录”的方法. 待我发现有种存储方式叫NSUserDefaults的时候,立马又感觉新技能get了. 简介: NSUserDefault ...
- 使用enum建立简单的状态机
Overview The enum in Java is more powerful than many other languages which can lead to surprising us ...
- AngularJs ngInclude、ngTransclude
这两个都是HTML DOM嵌入指令 ngInclude 读取,编译和插入外部的HTML片段. 格式:ng-include=“value”<ng-include src=”value” onloa ...