From b7a62cbf9d531da4745827698adc179f232f88fe Mon Sep 17 00:00:00 2001 From: "A. Murat Eren" Date: Sat, 30 Jul 2005 10:12:39 +0000 Subject: [PATCH] http + https icin resume destegi.. ftp'nin cok fazla isi oldugu icin biraz erteliyorum. --- pisi/fetcher.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/pisi/fetcher.py b/pisi/fetcher.py index 6d161081..cbf1d02e 100644 --- a/pisi/fetcher.py +++ b/pisi/fetcher.py @@ -9,6 +9,7 @@ # python standard library modules import urllib2 import os +from urllib import addinfourl from base64 import encodestring # pisi modules @@ -41,6 +42,7 @@ class Fetcher: self.percent = 0 self.rate = 0.0 self.progress = None + self.existSize = 0 def fetch (self): """Return value: Fetched file's full path..""" @@ -99,7 +101,7 @@ class Fetcher: if not os.access(url.path(), os.F_OK): self.err("no such file or no perm to read") - dest = open(os.path.join(self.filedest, url.filename()) , "w") + dest = open(os.path.join(self.filedest, url.filename()) , "wb") totalsize = os.path.getsize(url.path()) fileObj = open(url.path()) self.doGrab(fileObj, dest, totalsize) @@ -107,9 +109,17 @@ class Fetcher: def fetchRemoteFile (self): from httplib import HTTPException + localFile = os.path.join(self.filedest, self.url.filename()) + + if os.path.exists(localFile) and self.url.scheme() == "http" or self.url.scheme() == "https": + self.existSize = os.path.getsize(localFile) + ui.info('Resuming (%d bytes already downloaded)...\n' % self.existSize) + dest = open(localFile, "ab") + else: + dest = open(localFile, "wb") + try: - fileObj = urllib2.urlopen(self.formatRequest\ - (urllib2.Request(self.url.uri))) + fileObj = urllib2.urlopen(self.formatRequest(urllib2.Request(self.url.uri))) headers = fileObj.info() except ValueError, e: @@ -125,7 +135,6 @@ class Fetcher: totalsize = 0 # could not get the totalsize of file else: totalsize = int(headers['Content-Length']) - dest = open(os.path.join(self.filedest, self.url.filename()) , "w") self.doGrab(fileObj, dest, totalsize) def formatRequest(self, request): @@ -133,8 +142,29 @@ class Fetcher: if authinfo: enc = encodestring("%s:%s" % authinfo) request.add_header('Authorization', 'Basic %s' % enc) + if self.existSize: + range_handler = HTTPRangeHandler() + opener = urllib2.build_opener(range_handler) + urllib2.install_opener(opener) + request.add_header('Range', 'bytes=%d-' % self.existSize) return request def err (self, error): raise FetchError(error) +class HTTPRangeHandler(urllib2.BaseHandler): + """ + "urllib2, the 'Error 206: Partial Content' + reponse from the HTTP server is really what we expected. + Don't give up, resume the download.. + """ + + def http_error_206(self, request, fp, errcode, msg, headers): + return addinfourl(fp, headers, request.get_full_url()) + + def http_error_416(self, request, fp, errcode, msg, headers): + # HTTP 1.1's 'Range Not Satisfiable' error.. + raise FetchError('Requested Range Not Satisfiable') + +class FTPRangeHandler: + pass