finally: chunk.close() def close(self): """ cleanup """ for chunk in self.chunks: self.closeChunk(chunk) self.chunks = [] if hasattr(self, "m"): self.m.close() del self.m if hasattr(self, "cj"): del self.cj if hasattr(self, "info"): del self.info if __name__ == "__main__": url = "http://speedtest.netcologne.de/test_100mb.bin" from Bucket import Bucket bucket = Bucket() bucket.setRate(200 * 1024) bucket = None print "starting" dwnld = HTTPDownload(url, "test_100mb.bin", bucket=bucket) dwnld.download(chunks=3, resume=True)
class RequestFactory: def __init__(self, core): self.core = core self.bucket = Bucket() self.updateBucket() self.core.evm.listenTo("config:changed", self.updateConfig) def getURL(self, *args, **kwargs): """ see HTTPRequest for argument list """ h = DefaultRequest(self.getConfig()) try: rep = h.load(*args, **kwargs) finally: h.close() return rep ########## old api methods above def getRequest(self, context=None, klass=DefaultRequest): """ Creates a request with new or given context """ # also accepts the context class directly if isinstance(context, klass.CONTEXT_CLASS): return klass(self.getConfig(), context) elif context: return klass(*context) else: return klass(self.getConfig()) def getDownloadRequest(self, request=None, klass=DefaultDownload): """ Instantiates a instance for downloading """ # TODO: load with plugin manager return klass(self.bucket, request) def getInterface(self): return self.core.config["download"]["interface"] def getProxies(self): """ returns a proxy list for the request classes """ if not self.core.config["proxy"]["proxy"]: return {} else: type = "http" setting = self.core.config["proxy"]["type"].lower() if setting == "socks4": type = "socks4" elif setting == "socks5": type = "socks5" username = None if self.core.config["proxy"]["username"] and self.core.config["proxy"]["username"].lower() != "none": username = self.core.config["proxy"]["username"] pw = None if self.core.config["proxy"]["password"] and self.core.config["proxy"]["password"].lower() != "none": pw = self.core.config["proxy"]["password"] return { "type": type, "address": self.core.config["proxy"]["address"], "port": self.core.config["proxy"]["port"], "username": username, "password": pw, } def updateConfig(self, section, option, value): """ Updates the bucket when a config value changed """ if option in ("limit_speed", "max_speed"): self.updateBucket() def getConfig(self): """returns options needed for pycurl""" return {"interface": self.getInterface(), "proxies": self.getProxies(), "ipv6": self.core.config["download"]["ipv6"]} def updateBucket(self): """ set values in the bucket according to settings""" if not self.core.config["download"]["limit_speed"]: self.bucket.setRate(-1) else: self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024)
class RequestFactory(): def __init__(self, core): self.lock = Lock() self.core = core self.bucket = Bucket() self.updateBucket() self.cookiejars = {} def iface(self): return self.core.config["download"]["interface"] def getRequest(self, pluginName, account=None, type="HTTP"): self.lock.acquire() if type == "XDCC": return XDCCRequest(proxies=self.getProxies()) req = Browser(self.bucket, self.getOptions()) if account: cj = self.getCookieJar(pluginName, account) req.setCookieJar(cj) else: req.setCookieJar(CookieJar(pluginName)) self.lock.release() return req def getHTTPRequest(self, **kwargs): """ returns a http request, dont forget to close it ! """ options = self.getOptions() options.update(kwargs) # submit kwargs as additional options return HTTPRequest(CookieJar(None), options) def getURL(self, *args, **kwargs): """ see HTTPRequest for argument list """ h = HTTPRequest(None, self.getOptions()) try: rep = h.load(*args, **kwargs) finally: h.close() return rep def getCookieJar(self, pluginName, account=None): if (pluginName, account) in self.cookiejars: return self.cookiejars[(pluginName, account)] cj = CookieJar(pluginName, account) self.cookiejars[(pluginName, account)] = cj return cj def getProxies(self): """ returns a proxy list for the request classes """ if not self.core.config["proxy"]["proxy"]: return {} else: type = "http" setting = self.core.config["proxy"]["type"].lower() if setting == "socks4": type = "socks4" elif setting == "socks5": type = "socks5" username = None if self.core.config["proxy"]["username"] and self.core.config["proxy"]["username"].lower() != "none": username = self.core.config["proxy"]["username"] pw = None if self.core.config["proxy"]["password"] and self.core.config["proxy"]["password"].lower() != "none": pw = self.core.config["proxy"]["password"] return { "type": type, "address": self.core.config["proxy"]["address"], "port": self.core.config["proxy"]["port"], "username": username, "password": pw, } def getOptions(self): """returns options needed for pycurl""" return {"interface": self.iface(), "proxies": self.getProxies(), "ipv6": self.core.config["download"]["ipv6"]} def updateBucket(self): """ set values in the bucket according to settings""" if not self.core.config["download"]["limit_speed"]: self.bucket.setRate(-1) else: self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024)
chunk.close() def close(self): """ cleanup """ for chunk in self.chunks: self.closeChunk(chunk) self.chunks = [] if hasattr(self, "m"): self.m.close() del self.m if hasattr(self, "cj"): del self.cj if hasattr(self, "info"): del self.info if __name__ == "__main__": url = "http://speedtest.netcologne.de/test_100mb.bin" from Bucket import Bucket bucket = Bucket() bucket.setRate(200 * 1024) bucket = None print "starting" dwnld = HTTPDownload(url, "test_100mb.bin", bucket=bucket) dwnld.download(chunks=3, resume=True)
class RequestFactory: def __init__(self, core): self.core = core self.bucket = Bucket() self.updateBucket() self.core.evm.listenTo("config:changed", self.updateConfig) def getURL(self, *args, **kwargs): """ see HTTPRequest for argument list """ h = DefaultRequest(self.getConfig()) try: rep = h.load(*args, **kwargs) finally: h.close() return rep ########## old api methods above def getRequest(self, context=None, klass=DefaultRequest): """ Creates a request with new or given context """ # also accepts the context class directly if isinstance(context, klass.CONTEXT_CLASS): return klass(self.getConfig(), context) elif context: return klass(*context) else: return klass(self.getConfig()) def getDownloadRequest(self, request=None, klass=DefaultDownload): """ Instantiates a instance for downloading """ # TODO: load with plugin manager return klass(self.bucket, request) def getInterface(self): return self.core.config["download"]["interface"] def getProxies(self): """ returns a proxy list for the request classes """ if not self.core.config["proxy"]["proxy"]: return {} else: type = "http" setting = self.core.config["proxy"]["type"].lower() if setting == "socks4": type = "socks4" elif setting == "socks5": type = "socks5" username = None if self.core.config["proxy"]["username"] and self.core.config[ "proxy"]["username"].lower() != "none": username = self.core.config["proxy"]["username"] pw = None if self.core.config["proxy"]["password"] and self.core.config[ "proxy"]["password"].lower() != "none": pw = self.core.config["proxy"]["password"] return { "type": type, "address": self.core.config["proxy"]["address"], "port": self.core.config["proxy"]["port"], "username": username, "password": pw, } def updateConfig(self, section, option, value): """ Updates the bucket when a config value changed """ if option in ("limit_speed", "max_speed"): self.updateBucket() def getConfig(self): """returns options needed for pycurl""" return { "interface": self.getInterface(), "proxies": self.getProxies(), "ipv6": self.core.config["download"]["ipv6"] } def updateBucket(self): """ set values in the bucket according to settings""" if not self.core.config["download"]["limit_speed"]: self.bucket.setRate(-1) else: self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024)
class RequestFactory(): def __init__(self, core): self.lock = Lock() self.core = core self.bucket = Bucket() self.updateBucket() @property def iface(self): return self.core.config["download"]["interface"] def getRequest(self, pluginName, cj=None): # TODO: mostly obsolete, browser could be removed completely req = Browser(self.bucket, self.getOptions()) if cj: req.setCookieJar(cj) else: req.setCookieJar(CookieJar(pluginName)) return req def getHTTPRequest(self, **kwargs): """ returns a http request, don't forget to close it ! """ options = self.getOptions() options.update(kwargs) # submit kwargs as additional options return HTTPRequest(CookieJar(None), options) def getURL(self, *args, **kwargs): """ see HTTPRequest for argument list """ h = HTTPRequest(None, self.getOptions()) try: rep = h.load(*args, **kwargs) finally: h.close() return rep def openCookieJar(self, pluginname): """Create new CookieJar""" return CookieJar(pluginname) def getProxies(self): """ returns a proxy list for the request classes """ if not self.core.config["proxy"]["proxy"]: return {} else: type = "http" setting = self.core.config["proxy"]["type"].lower() if setting == "socks4": type = "socks4" elif setting == "socks5": type = "socks5" username = None if self.core.config["proxy"]["username"] and self.core.config["proxy"]["username"].lower() != "none": username = self.core.config["proxy"]["username"] pw = None if self.core.config["proxy"]["password"] and self.core.config["proxy"]["password"].lower() != "none": pw = self.core.config["proxy"]["password"] return { "type": type, "address": self.core.config["proxy"]["address"], "port": self.core.config["proxy"]["port"], "username": username, "password": pw, } def getOptions(self): """returns options needed for pycurl""" return {"interface": self.iface, "proxies": self.getProxies(), "ipv6": self.core.config["download"]["ipv6"]} def updateBucket(self): """ set values in the bucket according to settings""" if not self.core.config["download"]["limit_speed"]: self.bucket.setRate(-1) else: self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024)