def handle_response(response): if response.error and not isinstance(response.error, tornado.httpclient.HTTPError) \ or response.code == 599: self.set_status(500) self.write(''.join([ '<html><head><meta charset="utf8">', '<script>', 'setTimeout(function() { location.reload(); }, 1000);', '</script>', '</head><body>', '源服务器错误:<br>', str(response.error), response.body or '', '<p>正在为您自动重试...</p>', '</body></html>', ])) self.finish() else: self.set_status(response.code) for header in response.headers: if header not in [ 'Transfer-Encoding', # 防止 chunked 'Content-Encoding', 'Content-Length', # 防止gzip 'Etag', 'Expires', 'Last-Modified' # 防止缓存 ]: self.set_header(header, response.headers.get(header)) # 防止缓存 self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate') self.set_header('Pragma', 'no-cache') self.set_header('Expires', '0') # 多个Set-Cookie会在response.headers中被合并,这还原成多个Set-Cookie if header == 'Set-Cookie': raw_cookie_list = response.headers.get_list(header) if len(raw_cookie_list) == 1: self.set_header('Set-Cookie', response.headers.get(header)) elif len(raw_cookie_list) > 1: if not hasattr(self, "_new_cookie"): self._new_cookie = Cookie.SimpleCookie() for raw_cookie in raw_cookie_list: self._new_cookie.load(raw_cookie) if response.body: content_type = response.headers.get('Content-Type', '') if content_type and 'text/html' in content_type: body = process_html(response.body) elif content_type and 'text/css' in content_type: body = process_css(response.body) else: body = response.body self.set_header('Content-Length', len(body)) self.write(body) self.finish()
def get_content(cls, abspath, start=None, end=None): gc.collect() # 在mp4内容的时候,如果刷新网页会导致10053错误,并且内存不能回收,这里粗暴处理一下 _, ext = os.path.splitext(abspath) if ext in SPECIAL_EXTENSIONS: content = open(abspath, 'r').read() if ext in HTML_EXTENSIONS: return process_html(content) elif ext in CSS_EXTENSIONS: return process_css(content) return StaticFileHandler.get_content(abspath, start, end)
def handle_response(response): if response.error and not isinstance(response.error, tornado.httpclient.HTTPError) \ or response.code == 599: self.set_status(500) self.write(''.join([ '<html><head><meta charset="utf8">', '<script>', 'setTimeout(function() { location.reload(); }, 1000);', '</script>', '</head><body>', '源服务器错误:<br>', str(response.error), response.body or '', '<p>正在为您自动重试...</p>', '</body></html>', ])) self.finish() else: self.set_status(response.code) for header in response.headers: if header not in [ 'Transfer-Encoding', # 防止 chunked 'Content-Encoding', 'Content-Length', # 防止gzip 'Etag', 'Expires', 'Last-Modified', # 防止缓存 'Set-Cookie' # 后面cookie会另外处理 ]: self.set_header(header, response.headers.get(header)) # 防止缓存 self.set_header('Cache-Control', 'no-cache, no-store, must-revalidate') self.set_header('Pragma', 'no-cache') self.set_header('Expires', '0') # 多个Set-Cookie会在response.headers中被合并,这还原成多个Set-Cookie if header == 'Set-Cookie': raw_cookie_list = response.headers.get_list(header) if len(raw_cookie_list) == 1: self.set_header('Set-Cookie', response.headers.get(header)) elif len(raw_cookie_list) > 1: for raw_cookie in raw_cookie_list: self.add_header('Set-Cookie', raw_cookie) if response.body: content_type = response.headers.get('Content-Type', '') if content_type and 'text/html' in content_type: body = process_html(response.body) elif content_type and 'text/css' in content_type: body = process_css(response.body) else: body = response.body self.set_header('Content-Length', len(body)) self.write(body) self.finish()