用python做自动化测试--对服务器端的自动化测试(4)-接收端Http Server模拟器
来源:互联网 发布:苹果限免软件 编辑:程序博客网 时间:2024/05/17 23:23
Python有很多成熟的web框架,比如大名鼎鼎的Django和Pylons, 不过用这些来开发HTTP server模拟器,有点杀鸡用牛刀的意思,也比较复杂,所以web.py, Tornada是最好的选择,2者的用法基本类似。 我开始用的是web.py, 但web,py对于transfer-encoding: chunked的支持不好,当body很大的时候,会导致内存快速增加,溢出,需要打补丁才可以解决。 在搜索这个问题的解决方案的时候,看到了tornado, 性能是web.py的几十倍,用法是基本完全一样的,所以果断换成了Tornado。
#!/usr/bin/env python#coding=utf-8import loggingimport tornado.ioloopimport tornado.webdef get_headers(headers_value_file): headers_value_dic={} head_file=open(headers_value_file,"r") lines=head_file.readlines() head_file.close() for line in lines: if (line.split(":")[0]).strip()[0]!="#": headers_value_dic[(line.split(":")[0]).strip()]=(line.split(":")[1]).strip() return headers_value_dicclass register(tornado.web.RequestHandler): def get(self): self.write("registration is ok") def post(self): #得到全部的body数据,可以解析body的输入,构造reponse,这块涉及到公司业务,所以删除了, request_stream = self.request.body logging.debug("request:"+request_stream) #设置response header, 要设置哪些header和对应值,在register.header配置文件里面 for header, value in get_headers("register.header").iteritems(): self.set_header(header, value) rep=open("register.response","r") response=rep.read().strip() rep.close() self.write(self.write(response))class SMSVerification(tornado.web.RequestHandler): def get(self): self.write("SMSVerification is ok") def post(self): request_stream = self.request.body logging.debug("request:"+request_stream) response='''<?xml version="1.0" encoding="UTF-8"?> < SMSVerificationResponse > <ResponseCode>0</ResponseCode> <UserName>testuser</UserName> <UserPassword>12345</UserPassword> </ SMSVerificationResponse >''' self.write(response)if __name__=='__main__': application = tornado.web.Application([ (r"/register", register), (r"/SMSVerification", SMSVerification), ]) logging.basicConfig(filename="simserver.log",level=0,format='%(asctime)s %(name)s %(levelname)s %(module)s:%(lineno)d %(message)s') application.listen(8888) tornado.ioloop.IOLoop.instance().start()
这里的register对应前面client模拟器的reg响应, 返回给client的HTTP response。
register.header 文件例子
Content-Type: text/plain
Date:Mon, 04 Aug 2014 14:33:54 GMT
Connection:keep-alive
Vary:Accept-Encoding
register.response 文件例子
<?xml version="1.0" encoding="UTF-8"?>
< UserRegisterResponse >
<ResponseCode>0</ResponseCode>
<Description> Authentication success </Description>
</ UserRegisterResponse >
如果想要得到URL的参数,可以通过self.get_query_argument("username")
如果想要得到body里面的的参数,可以通过self.get_body_argument("username")
- 用python做自动化测试--对服务器端的自动化测试(4)-接收端Http Server模拟器
- 用python做自动化测试--对服务器端的自动化测试(2)-发送端Http Client模拟器
- 用python做自动化测试--对服务器端的自动化测试(3)-更多http client实例
- 用python做自动化测试--对服务器端的自动化测试(1)-系统架构
- 用python做http协议的api自动化测试一
- 用python做http协议的api自动化测试二
- 用python做http协议的api自动化测试三
- 用python做自动化测试--数据库的自动化测试(1)
- 用python做自动化测试--数据库的自动化测试(2)
- 用python做自动化测试--对Java代码做单元测试 (1)
- 测试HTTP接口的python自动化脚本
- 用python做自动化测试--序
- Python-Selenium2做Web自动化测试(4)-对象的定位
- 使用python和android模拟器做android程序的自动化测试
- 用python做自动化测试--Monkeyrunner 几个问题的解决方案
- 用python做自动化测试--Andriod App 的自动化-Appium的那些坑
- Python-Selenium2做Web自动化测试(2)-自动化测试常用工具
- 对非自动化测试的自动化支持
- 其他一些实现HTTP链接的类 AndroidHttpClient 和GoogleHttpClient
- 剑指offer之面试题36:数组中的逆序对-归并排序的应用
- UVa 269 Counting Patterns 解题报告(暴力)
- Linux下卸载Oracle
- 一台电脑同时运行多个tomcat配置方法
- 用python做自动化测试--对服务器端的自动化测试(4)-接收端Http Server模拟器
- linux内核学习书籍
- 1407311104-hd-人见人爱A^B.cpp
- RLConnection 和HttpURLConnection
- UITableViewController 编辑功能中的,添加,删除,修改
- HDU_1999 不可摸数
- 遍历给定文件夹中的所有文件以及文件夹 P79
- 天气预报网页版
- const函数