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

2024/10/5 15:27:11

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.

Answer

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/1024Response 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
https://en.xdnf.cn/q/70469.html

Related Q&A

AttributeError: module rest_framework.serializers has no attribute NullBooleanField

After upgrading djangorestframework from djangorestframework==3.13.1 to djangorestframework==3.14.0 the code from rest_framework.serializers import NullBooleanFieldThrowsAttributeError: module rest_fra…

Pandas convert dataframe values to column names

I want to use dataframe values as column names and simplify the dataframe.I tried df.stack() and then index.map({0[0]}_{0[1]}.format)Input_df(Got this df by doing a groupby):link price dateA 1 …

Pandas replace part of string with values from dictionary

I would like to replace the words in my dataframedf = pd.DataFrame({"Text": ["The quick brown fox jumps over the lazy dog"]})which match the keys in the following dictionarydic = {&…

Tensorflow autoencoder cost not decreasing?

I am working on unsupervised feature learning using autoencoders using Tensorflow. I have written following code for the Amazon csv dataset and when I am running it the cost is not decreasing at every …

Seconds since epoch to relative date

Im working with dates since epoch, and already got, for example:date = 6928727.56235Id like to transform this into another relative format, so that Ill be able to transform this into something relative…

ring buffer with numpy/ctypes

Im developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because its possible…

Get all available timezones

Im currently working on an application that is required to support multiple timezones.For that, Im using the dateutil library. Now, I need a way to present the user a list of all available timezones th…

Load blob image data into QPixmap

I am writing a program using PyQt4 for front-end GUI and this program accesses a back-end database (which can be either MySQL or SQLite). I need to store some image data in the database and below is th…

Fetch a value of SQLalchemy instrumentedattribute

How can I fetch the value of a InstrumentedAttribute object in SQLalchemy:(Pdb) ResultLine.item_reference_1 <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x793dc90>The above statemen…

python super calling child methods

There are numerous questions on the usage of super() but none of them appears to answer my question. When calling super().__init__() from a subclass, all method calls in the super-constructor are actua…