Python 3 resuma a file download

Ian Kelly ian.g.kelly at gmail.com
Wed Jul 1 11:10:15 EDT 2015


On Wed, Jul 1, 2015 at 8:18 AM,  <zljubisic at gmail.com> wrote:
> if I understood you correctly (I am not sure about which example you are refering), I should do the following:
> 1. check already downloaded file size in bytes = downloaded
> 2. url = 'http://video.hrt.hr/2906/otv296.mp4'
> 3. req = urllib.request.Request(url)
> 4. req.add_header('Range', downloaded)

You need to use the correct format for the Range header; see RFC 7233.
If you have 500 bytes and want the rest of the file, then the value
for the Range header would be "bytes=500-", not just "500". You can
build that string using string formatting, e.g.
"bytes={}-".format(downloaded)

> 5. urllib.request.urlretrieve(url, 'otv296.mp4')

A couple of problems with this. One is that it doesn't use the Request
object that you just constructed, so it wouldn't pass the Range
header. The other is that it will overwrite that file, not append to
it. You should use the urllib.request.urlopen function, and pass it
the Request object rather than the URL. You can then open your local
file in append mode, read the file data from the HTTPResponse object
returned by urlopen, and write it to the local file.



More information about the Python-list mailing list