2010-01-07 104 views
7

我正在用Python編程一個軟件,以從數據庫下載HTTP PDF。 有時下載停止此消息:使用Python中的urllib使用wget -c功能下載文件

retrieval incomplete: got only 3617232 out of 10689634 bytes 

我怎麼能要求下載重新啓動它停止使用206 Partial Content HTTP功能?

我可以使用wget -c來完成它,它工作得很好,但我想直接在我的Python軟件中實現它。

有什麼想法?

謝謝

回答

7

您可以通過發送GET與Range頭請求部分下載:

import urllib2 
req = urllib2.Request('http://www.python.org/') 
# 
# Here we request that bytes 18000--19000 be downloaded. 
# The range is inclusive, and starts at 0. 
# 
req.headers['Range'] = 'bytes=%s-%s' % (18000, 19000) 
f = urllib2.urlopen(req) 
# This shows you the *actual* bytes that have been downloaded. 
range=f.headers.get('Content-Range') 
print(range) 
# bytes 18000-18030/18031 
print(repr(f.read())) 
# ' </div>\n</body>\n</html>\n\n\n\n\n\n\n' 

要注意檢查Content-Range學習字節實際上已經被下載了什麼,因爲你的範圍可能超出界限,和/或不是所有的服務器似乎都尊重Range標題。