Skip to content

Instantly share code, notes, and snippets.

@lizhiwei
Created December 10, 2013 04:27
Show Gist options
  • Save lizhiwei/7885719 to your computer and use it in GitHub Desktop.
Save lizhiwei/7885719 to your computer and use it in GitHub Desktop.
Implementing HTTP 206 Partial Content support for web.py
###https://code.google.com/p/rctk/source/browse/core/trunk/rctk/webpy.py?r=447#57
## experimental partial content support
## perhaps this shouldn't be enabled by default
range = web.ctx.env.get('HTTP_RANGE')
if range is None:
return result
total = len(result)
_, r = range.split("=")
partial_start, partial_end = r.split("-")
start = int(partial_start)
if not partial_end:
end = total-1
else:
end = int(partial_end)
chunksize = (end-start)+1
web.ctx.status = "206 Partial Content"
web.header("Content-Range", "bytes %d-%d/%d" % (start, end, total))
web.header("Accept-Ranges", "bytes")
web.header("Content-Length", chunksize)
return result[start:end+1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment