Search code examples
pythonpython-2.7http-headershttp-protocols

Python: How to download file using range of bytes?


I want to download file in multi thread mode and I have following code here:

#!/usr/bin/env python

import httplib


def main():
    url_opt = '/film/0d46e21795209bc18e9530133226cfc3/7f_Naruto.Uragannie.Hroniki.001.seriya.a1.20.06.13.mp4'

    headers = {}
    headers['Accept-Language'] = 'en-GB,en-US,en'
    headers['Accept-Encoding'] = 'gzip,deflate,sdch'
    headers['Accept-Charset'] = 'max-age=0'
    headers['Cache-Control'] = 'ISO-8859-1,utf-8,*'
    headers['Cache-Control'] = 'max-age=0'
    headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1)'
    headers['Connection'] = 'keep-alive'
    headers['Accept'] = 'text/html,application/xhtml+xml,application/xml,*/*'
    headers['Range'] = ''

    conn = httplib.HTTPConnection('data09-cdn.datalock.ru:80')
    conn.request("GET", url_opt, '', headers)

    print "Request sent"

    resp = conn.getresponse()
    print resp.status
    print resp.reason
    print resp.getheaders()

    file_for_wirte = open('cartoon.mp4', 'w')
    file_for_wirte.write(resp.read())

    print resp.read()

    conn.close()


if __name__ == "__main__":
    main()

Here is output:

Request sent
200
OK
[('content-length', '62515220'), ('accept-ranges', 'bytes'), ('server', 'nginx/1.2.7'), ('last-modified', 'Thu, 20 Jun 2013 12:10:43 GMT'), ('connection', 'keep-alive'), ('date', 'Fri, 14 Feb 2014 07:53:30 GMT'), ('content-type', 'video/mp4')]

This code working perfectly however I do not understand through the documentation how to download file using ranges. If you see output of response, which server provides:

 ('content-length', '62515220'), ('accept-ranges', 'bytes')

It supports range in 'bytes' unit where content size is 62515220

However in this request whole file downloaded. But what I want to do first obtain server information like does this file can be supported using http range queries and content size of file with out downloading? And how I can create http query with range (i.e.: 0~25000)?


Solution

  • Pass Range header with bytes=start_offset-end_offset as range specifier.

    For example, following code retrieve the first 300 bytes. (0-299):

    >>> import httplib
    >>> conn = httplib.HTTPConnection('localhost')
    >>> conn.request("GET", '/', headers={'Range': 'bytes=0-299'}) # <----
    >>> resp = conn.getresponse()
    >>> resp.status
    206
    >>> resp.status == httplib.PARTIAL_CONTENT
    True
    >>> resp.getheader('content-range')
    'bytes 0-299/612'
    >>> content = resp.read()
    >>> len(content)
    300
    

    NOTE Both start_offset, end_offset are inclusive.

    UPDATE

    If the server does not understand Range header, it will respond with the status code 200 (httplib.OK) instead of 206 (httplib.PARTIAL_CONTENT), and it will send whole content. To make sure the server respond partial content, check the status code.

    >>> resp.status == httplib.PARTIAL_CONTENT
    True