cached-with-validation.py (574B)
1 ETAG = b'"123abc"' 2 CONTENT_TYPE = b"text/plain" 3 CONTENT = u"lorem ipsum dolor sit amet" 4 5 def main(request, response): 6 # let caching kick in if possible (conditional GET) 7 etag = request.headers.get(b"If-None-Match", None) 8 if etag == ETAG: 9 response.headers.set(b"X-HTTP-STATUS", 304) 10 response.status = (304, b"Not Modified") 11 return u"" 12 13 # cache miss, so respond with the actual content 14 response.status = (200, b"OK") 15 response.headers.set(b"ETag", ETAG) 16 response.headers.set(b"Content-Type", CONTENT_TYPE) 17 return CONTENT