Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `pause`, and `resume` download work?

Usually, downloading a file from the server is something like this:

fp = open(file, 'wb')
req = urllib2.urlopen(url)
for line in req:
    fp.write(line)
fp.close()

During downloading, the download process just has to be finished. If the process is stopped or interrupted, the download process needs to start again. So, I would like to enable my program to pause and resume the download, how do I implement such? Thanks.

like image 936
DNB5brims Avatar asked Sep 03 '12 07:09

DNB5brims


1 Answers

The web server must support Range request header to allow pause/resume download:

Range: <unit>=<range-start>-<range-end>

Then the client can make a request with the Range header if he/she wants to retrieve the specified bytes, for example:

Range: bytes=0-1024

In this case the server can respond with a 200 OK indicating that it doesn't support Range requests, Or it can respond with 206 Partial Content like this:

HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Length: 1024
Content-Range: bytes 64-512/1024

Response body.... till 512th byte of the file

See:

  • Range request header
  • Content-Range response header
  • Accept-Ranges response header
  • 206 Partial Content
  • HTTP 1.1 specification
like image 117
freestyler Avatar answered Sep 30 '22 21:09

freestyler