当网络重新连接时,Python urllib2 resume download不起作用

时间:2011-08-05 22:29:13

标签: python urllib2 resume-download

我正在使用urllib2来制作一个恢复下载器,大致基于this方法。我可以结束程序并重新启动它,并从它停止的地方开始下载,下载文件的大小与一次下载的文件相同。

但是,我在禁用和重新启用网络时对其进行了测试,但未正确下载。文件大小最终会长于文件的大小,并且文件无法正常工作。有没有我错过的,或者这可能是一个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

1 个答案:

答案 0 :(得分:1)

我在IOError处理程序中添加了self._outfile.close()和self._netfile.close(),似乎已经修复了它。我想这个错误是由于在没有关闭它的情况下再次打开而引起的。