本文整理汇总了Python中werkzeug.wrappers.Response.content_encoding方法的典型用法代码示例。如果您正苦于以下问题:Python Response.content_encoding方法的具体用法?Python Response.content_encoding怎么用?Python Response.content_encoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.wrappers.Response
的用法示例。
在下文中一共展示了Response.content_encoding方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_vary_miss
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import content_encoding [as 别名]
def test_vary_miss(self):
with a.test_request_context('/foo', headers=(('accept-encoding', 'gzip'),)):
r = Response('foo')
r.vary.add('accept-encoding')
r.content_encoding = 'gzip'
self.s.cache_response(r)
with self.assertRaises(NoMatchingRepresentation):
with a.test_request_context('/foo'):
self.r.fetch_response()
示例2: _try_serve_page
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import content_encoding [as 别名]
def _try_serve_page(self, app, environ, request):
# Find a matching page.
req_page = get_requested_page(app, request.path)
# If we haven't found any good match, report all the places we didn't
# find it at.
qp = req_page.qualified_page
if qp is None:
msg = "Can't find path for '%s':" % request.path
raise MultipleNotFound(msg, req_page.not_found_errors)
# We have a page, let's try to render it.
render_ctx = PageRenderingContext(qp,
page_num=req_page.page_num,
force_render=True,
is_from_request=True)
if qp.route.is_generator_route:
qp.route.generator.prepareRenderContext(render_ctx)
# See if this page is known to use sources. If that's the case,
# just don't use cached rendered segments for that page (but still
# use them for pages that are included in it).
uri = qp.getUri()
entry = self._page_record.getEntry(uri, req_page.page_num)
if (qp.route.is_generator_route or entry is None or
entry.used_source_names):
cache_key = '%s:%s' % (uri, req_page.page_num)
app.env.rendered_segments_repository.invalidate(cache_key)
# Render the page.
rendered_page = render_page(render_ctx)
# Remember stuff for next time.
if entry is None:
entry = ServeRecordPageEntry(req_page.req_path, req_page.page_num)
self._page_record.addEntry(entry)
for pinfo in render_ctx.render_passes:
entry.used_source_names |= pinfo.used_source_names
# Start doing stuff.
page = rendered_page.page
rp_content = rendered_page.content
# Profiling.
if app.config.get('site/show_debug_info'):
now_time = time.perf_counter()
timing_info = (
'%8.1f ms' %
((now_time - app.env.start_time) * 1000.0))
rp_content = rp_content.replace(
'__PIECRUST_TIMING_INFORMATION__', timing_info)
# Build the response.
response = Response()
etag = hashlib.md5(rp_content.encode('utf8')).hexdigest()
if not app.debug and etag in request.if_none_match:
response.status_code = 304
return response
response.set_etag(etag)
response.content_md5 = etag
cache_control = response.cache_control
if app.debug:
cache_control.no_cache = True
cache_control.must_revalidate = True
else:
cache_time = (page.config.get('cache_time') or
app.config.get('site/cache_time'))
if cache_time:
cache_control.public = True
cache_control.max_age = cache_time
content_type = page.config.get('content_type')
if content_type and '/' not in content_type:
mimetype = content_type_map.get(content_type, content_type)
else:
mimetype = content_type
if mimetype:
response.mimetype = mimetype
if ('gzip' in request.accept_encodings and
app.config.get('site/enable_gzip')):
try:
with io.BytesIO() as gzip_buffer:
with gzip.open(gzip_buffer, mode='wt',
encoding='utf8') as gzip_file:
gzip_file.write(rp_content)
rp_content = gzip_buffer.getvalue()
response.content_encoding = 'gzip'
except Exception:
logger.error("Error compressing response, "
"falling back to uncompressed.")
response.set_data(rp_content)
return response
示例3: _try_serve_page
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import content_encoding [as 别名]
def _try_serve_page(self, app, environ, request):
# Try to find what matches the requested URL.
req_path, page_num = split_sub_uri(app, request.path)
routes = find_routes(app.routes, req_path)
if len(routes) == 0:
raise RouteNotFoundError("Can't find route for: %s" % req_path)
rendered_page = None
first_not_found = None
for route, route_metadata in routes:
try:
logger.debug("Trying to render match from source '%s'." %
route.source_name)
rendered_page = self._try_render_page(
app, route, route_metadata, page_num, req_path)
if rendered_page is not None:
break
except NotFound as nfe:
if first_not_found is None:
first_not_found = nfe
else:
raise SourceNotFoundError(
"Can't find path for: %s (looked in: %s)" %
(req_path, [r.source_name for r, _ in routes]))
# If we haven't found any good match, raise whatever exception we
# first got. Otherwise, raise a generic exception.
if rendered_page is None:
first_not_found = first_not_found or NotFound(
"This page couldn't be found.")
raise first_not_found
# Start doing stuff.
page = rendered_page.page
rp_content = rendered_page.content
# Profiling.
if app.config.get('site/show_debug_info'):
now_time = time.clock()
timing_info = (
'%8.1f ms' %
((now_time - app.env.start_time) * 1000.0))
rp_content = rp_content.replace(
'__PIECRUST_TIMING_INFORMATION__', timing_info)
# Build the response.
response = Response()
etag = hashlib.md5(rp_content.encode('utf8')).hexdigest()
if not app.debug and etag in request.if_none_match:
response.status_code = 304
return response
response.set_etag(etag)
response.content_md5 = etag
cache_control = response.cache_control
if app.debug:
cache_control.no_cache = True
cache_control.must_revalidate = True
else:
cache_time = (page.config.get('cache_time') or
app.config.get('site/cache_time'))
if cache_time:
cache_control.public = True
cache_control.max_age = cache_time
content_type = page.config.get('content_type')
if content_type and '/' not in content_type:
mimetype = content_type_map.get(content_type, content_type)
else:
mimetype = content_type
if mimetype:
response.mimetype = mimetype
if ('gzip' in request.accept_encodings and
app.config.get('site/enable_gzip')):
try:
with io.BytesIO() as gzip_buffer:
with gzip.open(gzip_buffer, mode='wt',
encoding='utf8') as gzip_file:
gzip_file.write(rp_content)
rp_content = gzip_buffer.getvalue()
response.content_encoding = 'gzip'
except Exception:
logger.exception("Error compressing response, "
"falling back to uncompressed.")
response.set_data(rp_content)
return response
示例4: _try_serve_page
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import content_encoding [as 别名]
def _try_serve_page(self, app, environ, request):
# Find a matching page.
req_pages, not_founds = get_requested_pages(app, request.path)
rendered_page = None
for req_page in req_pages:
# We have a page, let's try to render it.
render_ctx = RenderingContext(req_page.page,
sub_num=req_page.sub_num,
force_render=True)
req_page.page.source.prepareRenderContext(render_ctx)
# Render the page.
this_rendered_page = render_page(render_ctx)
# We might have rendered a page that technically exists, but
# has no interesting content, like a tag page for a tag that
# isn't used by any page. To eliminate these false positives,
# we check if there was pagination used, and if so, if it had
# anything in it.
# TODO: we might need a more generic system for other cases.
render_info = this_rendered_page.render_info
if (render_info['used_pagination'] and
not render_info['pagination_has_items']):
not_founds.append(PageNotFoundError(
("Rendered '%s' (page %d) in source '%s' "
"but got empty content:\n\n%s\n\n") %
(req_page.req_path, req_page.sub_num,
req_page.page.source.name, this_rendered_page.content)))
continue
rendered_page = this_rendered_page
break
# If we haven't found any good match, report all the places we didn't
# find it at.
if rendered_page is None:
msg = "Can't find path for '%s':" % request.path
raise MultipleNotFound(msg, not_founds)
# Start doing stuff.
page = rendered_page.page
rp_content = rendered_page.content
# Profiling.
if app.config.get('site/show_debug_info'):
now_time = time.perf_counter()
timing_info = (
'%8.1f ms' %
((now_time - app.env.start_time) * 1000.0))
rp_content = rp_content.replace(
'__PIECRUST_TIMING_INFORMATION__', timing_info)
# Build the response.
response = Response()
etag = hashlib.md5(rp_content.encode('utf8')).hexdigest()
if not app.debug and etag in request.if_none_match:
response.status_code = 304
return response
response.set_etag(etag)
response.content_md5 = etag
cache_control = response.cache_control
if app.debug:
cache_control.no_cache = True
cache_control.must_revalidate = True
else:
cache_time = (page.config.get('cache_time') or
app.config.get('site/cache_time'))
if cache_time:
cache_control.public = True
cache_control.max_age = cache_time
content_type = page.config.get('content_type')
if content_type and '/' not in content_type:
mimetype = content_type_map.get(content_type, content_type)
else:
mimetype = content_type
if mimetype:
response.mimetype = mimetype
if ('gzip' in request.accept_encodings and
app.config.get('site/enable_gzip')):
try:
with io.BytesIO() as gzip_buffer:
with gzip.open(gzip_buffer, mode='wt',
encoding='utf8') as gzip_file:
gzip_file.write(rp_content)
rp_content = gzip_buffer.getvalue()
response.content_encoding = 'gzip'
except Exception:
logger.error("Error compressing response, "
"falling back to uncompressed.")
response.set_data(rp_content)
return response