磁带存储和重放http请求。
cassette的Python项目详细描述
卡式磁带存储和回放在python应用程序中发出的http请求。
importurllib2importcassettewithcassette.play("data/responses.yaml"):# If the request is not already stored in responses.yaml, cassette# will request the URL and store its response in the file.r=urllib2.urlopen("http://www.internic.net/domain/named.root")# This time, the request response must be in the file. The external# request is not made. cassette retrieves the response from the# file.r=urllib2.urlopen("http://www.internic.net/domain/named.root")assert"A.ROOT-SERVERS.NET"inr.read(10000)
盒式磁带还支持requests 图书馆。
importrequestswithcassette.play("data/responses.yaml"):r=requests.get("http://www.internic.net/domain/named.root")
请注意,存储在不同库之间的请求可能与 彼此。也就是说,使用urllib2存储的请求可能仍然会触发外部 请求与使用requests请求的URL相同。
安装
$ pip install cassette