From d2e3afceb738af20aeb8e41f9aad12150cf1e8a7 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sun, 4 Dec 2011 13:39:42 +0100 Subject: Better download connection handling: Detect server error earlier, fallback to single connection if possible --- module/network/HTTPChunk.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'module/network/HTTPChunk.py') diff --git a/module/network/HTTPChunk.py b/module/network/HTTPChunk.py index 69eedb19c..582067aa8 100644 --- a/module/network/HTTPChunk.py +++ b/module/network/HTTPChunk.py @@ -16,7 +16,7 @@ @author: RaNaN """ -from os import remove, stat +from os import remove, stat, fsync from os.path import exists from time import sleep from re import search @@ -146,6 +146,9 @@ class HTTPChunk(HTTPRequest): self.sleep = 0.000 self.lastSize = 0 + def __repr__(self): + return "" % (self.id, self.size, self.arrived) + @property def cj(self): return self.p.cj @@ -157,7 +160,7 @@ class HTTPChunk(HTTPRequest): self.c.setopt(pycurl.WRITEFUNCTION, self.writeBody) self.c.setopt(pycurl.HEADERFUNCTION, self.writeHeader) - # request one byte more, since some servers in russia seems to have a defect arihmetic unit + # request all bytes, since some servers in russia seems to have a defect arihmetic unit if self.resume: self.fp = open(self.p.info.getChunkName(self.id), "ab") @@ -259,10 +262,25 @@ class HTTPChunk(HTTPRequest): self.headerParsed = True + def stop(self): + """The download will not proceed after next call of writeBody""" + self.range = [0,0] + self.size = 0 + + def resetRange(self): + """ Reset the range, so the download will load all data available """ + self.range = None + def setRange(self, range): self.range = range self.size = range[1] - range[0] + def flushFile(self): + """ flush and close file """ + self.fp.flush() + fsync(self.fp.fileno()) #make sure everything was written to disk + self.fp.close() #needs to be closed, or merging chunks will fail + def close(self): """ closes everything, unusable after this """ if self.fp: self.fp.close() -- cgit v1.2.3