2
我使用urllib2來恢復下載器,大致基於this方法。我可以結束程序並重新啓動程序,並從停止的地方開始下載,下載最終以相同大小下載的文件,就像它一次下載完成一樣。當網絡重新連接時,Python urllib2恢復下載不起作用
但是,在禁用和重新啓用網絡時,我已經對它進行了測試,並且沒有正確下載。文件大小最終比文件應該長,文件無法正常工作。有沒有我錯過了,或者這可能是一個urllib2錯誤?
import urllib2
opener = urllib2.build_opener();
self.count = 0 # Counts downloaded size.
self.downloading = True
while (not(self.success) and self.downloading):
try:
self.Err = ""
self._netfile = self.opener.open(self.url)
self.filesize = float(self._netfile.info()['Content-Length'])
if (os.path.exists(self.localfile) and os.path.isfile(self.localfile)):
self.count = os.path.getsize(self.localfile)
print self.count,"of",self.filesize,"downloaded."
if self.count >= self.filesize:
#already downloaded
self.downloading = False
self.success = True
self._netfile.close()
return
if (os.path.exists(self.localfile) and os.path.isfile(self.localfile)):
#File already exists, start where it left off:
#This seems to corrupt the file sometimes?
self._netfile.close()
req = urllib2.Request(self.url)
print "file downloading at byte: ",self.count
req.add_header("Range","bytes=%s-" % (self.count))
self._netfile = self.opener.open(req)
if (self.downloading): #Don't do it if cancelled, downloading=false.
next = self._netfile.read(1024)
self._outfile = open(self.localfile,"ab") #to append binary
self._outfile.write(next)
self.readsize = desc(self.filesize) # get size mb/kb
self.count += 1024
while (len(next)>0 and self.downloading):
next = self._netfile.read(1024)
self._outfile.write(next)
self.count += len(next)
self.success = True
except IOError, e:
print e
self.Err=("Download error, retrying in a few seconds: "+str(e))
try:
self._netfile.close()
except Exception:
pass
time.sleep(8) #Then repeat
已經有(幾乎)插入urllib替換它可以恢復:http://urlgrabber.baseurl.org/ –
你有沒有嘗試過禁用/啓用網絡?它會自動重新下載嗎? – NoBugs
我認爲它被內部的一些Linux軟件包管理使用,所以它應該被很好的測試 - 我很久以前就成功地使用它了。它甚至有一個重試次數設置等。 –