progressive-image.py (852B)
1 import os.path 2 import time 3 4 from wptserve.utils import isomorphic_encode 5 6 def main(request, response): 7 name = request.GET.first(b"name") 8 sleepTime = float(request.GET.first(b"sleep")) / 1E3 9 numInitial = int(request.GET.first(b"numInitial")) 10 11 path = os.path.join(os.path.dirname(isomorphic_encode(__file__)), name) 12 body = open(path, u"rb").read() 13 14 response.headers.set(b"Content-Type", b"image") 15 response.headers.set(b"Content-Length", len(body)) 16 response.headers.set(b"Cache-control", b"no-cache, must-revalidate") 17 response.write_status_headers() 18 19 # Read from the beginning, |numInitial| bytes. 20 first = body[:numInitial] 21 response.writer.write_content(first) 22 23 time.sleep(sleepTime) 24 25 # Read the remainder after having slept. 26 second = body[numInitial:] 27 response.writer.write_content(second)