def https_open(self, request):
global disable_network
if disable_network:
raise URLError('Network access is disabled')
url = request.get_full_url()
data = GetRequestData(request)
digest = DigestFromRequest(request)
response_file_path = os.path.join(RESPONSE_DATA_DIR, digest)
requests_seen.append(request)
if os.path.exists(response_file_path):
f = open(response_file_path, mode='rb')
m = Message()
m.add_header('Content-Type', 'application/json')
resp = addinfourl(f, headers=m, url=url, code=200)
resp.msg = 'Success'
resp.code = 200
return resp
# The file was not there. Create a missing resource file.
missing_response_file_path = os.path.join(RESPONSE_DATA_DIR,
'{}.missing'.format(digest))
with open(missing_response_file_path, mode='wb') as f:
s = json.dumps({
"url": url,
"data": StringFromBytes(data),
"digest": digest
})
f.write(s.encode('utf-8'))
raise URLError(
'URL is not cached. Created missing request record: {}.missing'.format(
digest))
评论列表
文章目录