我想以多线程模式下载文件,这里有以下代码:#!/usr/bin/env python
import httplib
def main():
url_opt = '/film/0d46e21795209bc18e9530133226cfc3/7f_Naruto.Uragannie.Hroniki.001.seriya.a1.20.06.13.mp4'
headers = {}
headers['Accept-Language'] = 'en-GB,en-US,en'
headers['Accept-Encoding'] = 'gzip,deflate,sdch'
headers['Accept-Charset'] = 'max-age=0'
headers['Cache-Control'] = 'ISO-8859-1,utf-8,*'
headers['Cache-Control'] = 'max-age=0'
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1)'
headers['Connection'] = 'keep-alive'
headers['Accept'] = 'text/html,application/xhtml+xml,application/xml,*/*'
headers['Range'] = ''
conn = httplib.HTTPConnection('data09-cdn.datalock.ru:80')
conn.request("GET", url_opt, '', headers)
print "Request sent"
resp = conn.getresponse()
print resp.status
print resp.reason
print resp.getheaders()
file_for_wirte = open('cartoon.mp4', 'w')
file_for_wirte.write(resp.read())
print resp.read()
conn.close()
if __name__ == "__main__":
main()
这里是输出:Request sent
200
OK
[('content-length', '62515220'), ('accept-ranges', 'bytes'), ('server', 'nginx/1.2.7'), ('last-modified', 'Thu, 20 Jun 2013 12:10:43 GMT'), ('connection', 'keep-alive'), ('date', 'Fri, 14 Feb 2014 07:53:30 GMT'), ('content-type', 'video/mp4')]
这段代码工作得很好,但是我不明白如何通过文档下载文件使用范围。如果您看到响应的输出,哪个服务器提供:('content-length', '62515220'), ('accept-ranges', 'bytes')
它支持以字节为单位的范围,内容大小为6251520
但是在这个请求中整个文件都被下载了。但是我想做的是首先获取服务器信息,比如这个文件是否可以使用http范围查询和文件的内容大小来支持,而不需要下载?如何创建范围为0~25000的http查询?