本文整理汇总了Python中werkzeug.wrappers.Response.last_modified方法的典型用法代码示例。如果您正苦于以下问题:Python Response.last_modified方法的具体用法?Python Response.last_modified怎么用?Python Response.last_modified使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.wrappers.Response
的用法示例。
在下文中一共展示了Response.last_modified方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_file_response
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def get_file_response(self, path, request):
try:
full_path = find_file(self.search_paths, path)
if full_path is None:
raise NotFound()
file_obj = open(full_path, 'rb')
mtime = get_file_mtime(full_path)
fsize = os.path.getsize(full_path)
except (ValueError, IOError, OSError):
raise Forbidden()
mimetype, encoding = mimetypes.guess_type(full_path)
if not mimetype:
peeked = peek_file(file_obj, 1024)
is_binary = is_binary_string(peeked)
if peeked and is_binary:
mimetype = self.default_binary_mime
else:
mimetype = self.default_text_mime # TODO: binary
resp = Response('')
cached_mtime = request.if_modified_since
if self.cache_timeout:
resp.cache_control.public = True
if mtime == cached_mtime:
file_obj.close()
resp.status_code = 304
resp.cache_control.max_age = self.cache_timeout
return resp
file_wrapper = request.environ.get('wsgi.file_wrapper', FileWrapper)
resp.response = file_wrapper(file_obj)
resp.content_type = mimetype
resp.content_length = fsize
resp.last_modified = mtime
return resp
示例2: test_date_no_clobber
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def test_date_no_clobber(self):
r = Response()
r.date = 0
r.last_modified = 0
v.add_date_fields(r)
self.assertEquals(r.date.year, 1970)
self.assertEquals(r.last_modified.year, 1970)
示例3: serve_full_file_request
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def serve_full_file_request(request, headers, file, callbacks=[]):
headers.update({
'Content-Length': file.length,
'Accept-Ranges': 'bytes',
})
file_iterator = wrap_file(request.environ, file)
response = Response(ClosingIterator(file_iterator, callbacks=callbacks),
direct_passthrough=True, mimetype=file.content_type,
headers=headers)
response.last_modified = file.uploadDate
response.set_etag(file.md5)
return response
示例4: file_handler
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def file_handler(self, environ, filename):
path = os.path.join(pkg_resources.resource_filename("cms.web", "practice"), filename)
response = Response()
response.status_code = 200
response.mimetype = "application/octet-stream"
mimetype = mimetypes.guess_type(filename)[0]
if mimetype is not None:
response.mimetype = mimetype
response.last_modified = datetime.utcfromtimestamp(os.path.getmtime(path)).replace(microsecond=0)
response.response = wrap_file(environ, io.open(path, "rb"))
response.direct_passthrough = True
return response
示例5: wsgi_app
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def wsgi_app(self, environ, start_response):
request = Request(environ)
request.encoding_errors = "strict"
response = Response()
response.status_code = 200
response.mimetype = "text/html"
response.last_modified = \
datetime.utcfromtimestamp(os.path.getmtime(self.path))\
.replace(microsecond=0)
# TODO check for If-Modified-Since and If-None-Match
response.response = wrap_file(environ, open(self.path, 'rb'))
response.direct_passthrough = True
return response
示例6: send_file
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def send_file(request, filename):
mimetype = mimetypes.guess_type(filename)[0]
if mimetype is None:
mimetype = 'application/octet-stream'
headers = Headers()
try:
file = open(filename, 'rb')
mtime = os.path.getmtime(filename)
headers['Content-Length'] = os.path.getsize(filename)
except (IOError, OSError):
raise NotFound()
rewritten = False
if mimetype == 'text/html':
rewritten = True
file = rewrite_html_for_editing(file,
edit_url=posixpath.join('/', request.script_root, 'admin/edit'))
del headers['Content-Length']
headers['Cache-Control'] = 'no-cache, no-store'
data = wrap_file(request.environ, file)
rv = Response(data, mimetype=mimetype, headers=headers,
direct_passthrough=True)
if not rewritten:
# if we know the file modification date, we can store it as
# the time of the last modification.
if mtime is not None:
rv.last_modified = int(mtime)
rv.cache_control.public = True
try:
rv.set_etag('lektor-%s-%s-%s' % (
os.path.getmtime(filename),
os.path.getsize(filename),
adler32(
filename.encode('utf-8') if isinstance(filename, basestring)
else filename
) & 0xffffffff,
))
except OSError:
pass
return rv
示例7: render_static
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def render_static(self, request, fileobj):
if request.method != 'GET':
raise http.MethodNotAllowed(['GET'])
content_type, _ = mimetypes.guess_type(fileobj.name)
stat = os.stat(fileobj.name)
etag = hashlib.md5(str(stat.st_mtime)).hexdigest()
response = Response(fileobj, mimetype=content_type)
response.content_length = stat.st_size
response.add_etag(etag)
response.last_modified = datetime.utcfromtimestamp(stat.st_mtime)
response.expires = datetime.utcfromtimestamp(stat.st_mtime + self.static_expires)
response.make_conditional(request)
return response
示例8: _make_wrapped_file_response
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def _make_wrapped_file_response(self, environ, request, path):
logger.debug("Serving %s" % path)
# Check if we can return a 304 status code.
mtime = os.path.getmtime(path)
etag_str = '%s$$%s' % (path, mtime)
etag = hashlib.md5(etag_str.encode('utf8')).hexdigest()
if etag in request.if_none_match:
response = Response()
response.status_code = 304
return response
wrapper = wrap_file(environ, open(path, 'rb'))
response = Response(wrapper)
_, ext = os.path.splitext(path)
response.set_etag(etag)
response.last_modified = datetime.datetime.fromtimestamp(mtime)
response.mimetype = self._mimetype_map.get(
ext.lstrip('.'), 'text/plain')
return response
示例9: make_wrapped_file_response
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def make_wrapped_file_response(environ, request, path):
# Check if we can return a 304 status code.
mtime = os.path.getmtime(path)
etag_str = '%s$$%s' % (path, mtime)
etag = hashlib.md5(etag_str.encode('utf8')).hexdigest()
if etag in request.if_none_match:
logger.debug("Serving %s [no download, E-Tag matches]" % path)
response = Response()
response.status_code = 304
return response
logger.debug("Serving %s [full download]" % path)
wrapper = wrap_file(environ, open(path, 'rb'))
response = Response(wrapper)
_, ext = os.path.splitext(path)
response.set_etag(etag)
response.last_modified = datetime.datetime.fromtimestamp(mtime)
response.mimetype = mimetype_map.get(
ext.lstrip('.'), 'text/plain')
response.direct_passthrough = True
return response
示例10: wsgi_app
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def wsgi_app(self, environ, start_response):
route = self.router.bind_to_environ(environ)
try:
endpoint, args = route.match()
except HTTPException as exc:
return exc
location = self.location % args
request = Request(environ)
request.encoding_errors = "strict"
response = Response()
available = list()
for extension, mimetype in self.EXT_TO_MIME.iteritems():
if os.path.isfile(location + '.' + extension):
available.append(mimetype)
mimetype = request.accept_mimetypes.best_match(available)
if mimetype is not None:
path = "%s.%s" % (location, self.MIME_TO_EXT[mimetype])
else:
path = self.fallback
mimetype = 'image/png' # FIXME Hardcoded type.
response.status_code = 200
response.mimetype = mimetype
response.last_modified = \
datetime.utcfromtimestamp(os.path.getmtime(path))\
.replace(microsecond=0)
# TODO check for If-Modified-Since and If-None-Match
response.response = wrap_file(environ, io.open(path, 'rb'))
response.direct_passthrough = True
return response
示例11: images
# 需要导入模块: from werkzeug.wrappers import Response [as 别名]
# 或者: from werkzeug.wrappers.Response import last_modified [as 别名]
def images(self, slug, max_width=None, max_height=None, *args, **kwargs):
cr, context = request.cr, request.context
# buscamos imagen por slug
img, model = self._get_image_gallery(slug)
if not img:
return request.not_found()
# leemos imagen
[record] = model.read(
cr, SUPERUSER_ID, [img[0]],
['name', 'last_update_img'], context=context)
path_file = os.path.join(_filestorage(cr), record.get('name'))
if not path_file:
return request.not_found()
# generamos respuesta
server_format = misc.DEFAULT_SERVER_DATETIME_FORMAT
response = Response(mimetype='image/jpg')
response.last_modified = datetime.datetime.strptime(
record.get('last_update_img'), server_format)
response.make_conditional(request.httprequest)
if response.status_code == 304:
return response
# si no hay tamaño la original
if (not max_width) and (not max_height):
data = self._read_image_data(path_file)
response.set_etag(hashlib.sha1(data).hexdigest())
response.data = data
return response
# creamos thumb si no existe
path, file_name = os.path.split(path_file)
path_file_thumb = os.path.join(path,
'{}x{}'.format(max_width, max_height))
if not os.path.exists(path_file_thumb):
try:
os.makedirs(path_file_thumb)
except:
_log.error(u"ERROR creando directorio para galerias <{}>"
.format(slug))
return response
path_file_thumb = os.path.join(path_file_thumb, file_name)
if os.path.exists(path_file_thumb):
data = self._read_image_data(path_file_thumb)
response.set_etag(hashlib.sha1(data).hexdigest())
response.data = data
return response
# creamos thumb
data = self._read_image_data(path_file)
response.set_etag(hashlib.sha1(data).hexdigest())
image = Image.open(cStringIO.StringIO(data))
response.mimetype = Image.MIME[image.format]
w, h = image.size
max_w = int(max_width) if max_width else maxint
max_h = int(max_height) if max_height else maxint
# guardamos en disco
image.thumbnail((max_w, max_h), Image.ANTIALIAS)
image.save(path_file_thumb, image.format)
response.data = self._read_image_data(path_file_thumb)
return response