Created
January 27, 2018 17:59
-
-
Save singhpratyush/b9aa44760d55ad805d1e6a4fa95639eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r = requests.get(url) | |
range_header = request.headers.get('Range', None) | |
if range_header: # Client has requested for partial content | |
size = int(r.headers.get('content-length')) # Actual size of song | |
# Look up for ranges | |
m = re.search('(\d+)-(\d*)', range_header) | |
g = m.groups() | |
byte1, byte2 = 0, None | |
if g[0]: | |
byte1 = int(g[0]) | |
if g[1]: | |
byte2 = int(g[1]) | |
length = size - byte1 | |
if byte2: | |
length = byte2 + 1 - byte1 | |
data = r.content[byte1: byte2] # Trim the data from server | |
# Prepare response | |
rv = Response(data, 206, mimetype=mime, direct_passthrough=True) | |
rv.headers.add('Content-Range', 'bytes {0}-{1}/{2}'.format(byte1, byte1 + length - 1, size)) | |
return rv | |
# No partial content, handle normally |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment