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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With