如何暂停和恢复下载工作?
通常,从服务器下载文件是这样的:
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.
Web服务器必须支持范围请求才能允许暂停/继续下载.
The web server must support range request to allow pause/resume download.
如果客户想要检索指定的字节,则在请求中添加一个Range标头:
The client adds a Range header in the request if he wants to retrieve the specified bytes:
Range: bytes=0-999
服务器将返回部分内容响应,如下所示:
The server will return a partial content response like this:
HTTP/1.0 206 Partial Content
Accept-Ranges: bytes
Content-Length: 1000
Content-Range: bytes 0-999/2200
balabalaa....