使用多个线程下载单个文件 [英] Download A Single File Using Multiple Threads

查看:113
本文介绍了使用多个线程下载单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Linux创建一个下载管理器,让我可以使用多个线程下载一个文件。这是我要做的:

I'm trying to create a 'Download Manager' for Linux that lets me download one single file using multiple threads. This is what I'm trying to do :


  1. 通过指定偏移量将要下载的文件分成不同的部分

  2. 将不同的部分下载到临时位置

  3. 将它们合并到一个文件中。

步骤2和3是可解的,而在步骤#1,我被卡住了。下载文件时如何指定偏移量?

Steps 2 and 3 are solvable, and it is at Step #1 that I'm stuck. How do I specify an offset while downloading a file?

使用沿着 open(/ path / to / file,wb)行的东西write(urllib2.urlopen ).read())不让我指定要读取的起点。是否有任何替代方案?

Using something along the lines of open("/path/to/file", "wb").write(urllib2.urlopen(url).read()) does not let me specify a starting point to read from. Is there any alternative to this?

推荐答案

要下载部分文件,只需设置 Range 这样的标题

To download part of the file, just set the Range header like this

req = urllib2.Request(url)
req.headers['Range'] = 'bytes=%s-%s' % (start, end)
f = urllib2.urlopen(req)

不是所有的服务器都支持 Range 头。大多数文件共享服务都没有。

Not all server support the Range header though. Most file sharing service don't.

这篇关于使用多个线程下载单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆