用于记录和回放http交互的httplib注入库。
dalton的Python项目详细描述
- “我希望你和蔼可亲,直到不和蔼可亲的时候。”
- –道尔顿,路之家
用于记录和回放http交互的httplib注入库。
dalton monkey将httplib的httpconnection类的两个方法修补到 拦截请求/响应交互,并基于 道尔顿录音。为了简化测试,记录是由python代码生成的 轻松定制响应并允许 播放路径。
- httpconnection的猴子修补方法:
- 请求
- 获取响应
使用更详细的方法通过httpconnection发送/接收请求是 目前不支持。
注意:这是第一个早期版本,主要是为了使用它 具有录制/回放交互的机械化。因为机械化只使用 httpconnection上的request/getresponse api,我对添加 截住其他人。请随意用叉子叉这个 功能,因为我不打算自己添加它们(尽管我很乐意使用bug 通过单元测试修复和添加功能)。
警告:道尔顿使用inspect.currentframemagic派生调用方 它可能只对cpython有效(pypy和jython未经测试)。
示例
由于dalton monkey修补了httplib,所以不需要修改库 利用支持的方法。
import dalton dalton.inject() # monkey-patch httplib from httplib import HTTPConnection h = HTTPConnection('www.google.com') # when recording, httplib capture is restricted by caller recorder = dalton.Recorder(caller=h) # record httplib calls in this block with recorder.recording(): h.request('GET', '/') resp = h.getresponse() body = resp.read() # save the interaction recorder.save('google')
将在当前目录中创建名为google的文件夹以供使用 有道尔顿的回放设备。
回放:
import dalton dalton.inject() # monkey-patch httplib from httplib import HTTPConnection h = HTTPConnection('www.google.com') # load the player player = dalton.Player(caller=h, playback_dir='google') # run httplib calls against the player with player.playing(): h.request('GET', '/') resp = h.getresponse() body = resp.read() # body is now the same as it was recorded, no calls to www.google.com # were made
- 这将生成具有以下布局的目录google:
- __init__.py
- step_0_response.txt
__init__.py的内容包含以下生成的回放 信息:
import os import dalton from dalton import FileWrapper here = os.path.abspath(os.path.dirname(__file__)) class StepNumber0(object): recorded_request = { 'headers': {}, 'url': '/', 'method': 'GET', 'body': None, } recorded_response = { 'headers': [('x-xss-protection', '1; mode=block'), ('transfer-encoding', 'chunked'), ( 'set-cookie', 'PREF=ID=ff; expires=Thu, 11-Apr-2013 20:19:35 GMT; path=/; domain=.google.com, NID=45=fU; expires=Wed, 12-Oct-2011 20:19:35 GMT; path=/; domain=.google.com; HttpOnly'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Tue, 12 Apr 2011 20:19:35 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')], 'body': FileWrapper('step_0_response.txt', here), 'status': 200, 'reason': 'OK', 'version': 11, } next_step = 'None' def handle_request(self, request): assert dalton.request_match(request, self.recorded_request) return (self.next_step, dalton.create_response(self.recorded_response))
录制后可以修改此文件以自定义播放,添加 其他分支机构等
支架
道尔顿被认为是功能完整的,因为项目业主(Ben Bangert)已经 除了计划的bug修复之外,没有其他功能或开发。虫子可以 在github上归档,并附带一个测试用例以保持当前 代码覆盖率,并且在准备接受到 道尔顿密码库。