class NetworkAccessManager(QNetworkAccessManager): OPERATION_NAMES = {getattr(QNetworkAccessManager, '%sOperation'%x) : x.upper() for x in ('Head', 'Get', 'Put', 'Post', 'Delete', 'Custom') } def __init__(self, *args): QNetworkAccessManager.__init__(self, *args) self.current_root = None self.cache = QNetworkDiskCache(self) self.setCache(self.cache) self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(0) def createRequest(self, operation, request, data): url = unicode(request.url().toString(QUrl.None)) if operation == self.GetOperation and url.startswith('file://'): path = url[7:] if iswindows and path.startswith('/'): path = path[1:] c = current_container() try: name = c.abspath_to_name(path, root=self.current_root) except ValueError: # Happens on windows with absolute paths on different drives name = None if c.has_name(name): try: return NetworkReply(self, request, c.mime_map.get(name, 'application/octet-stream'), name) except Exception: import traceback traceback.print_exc() return QNetworkAccessManager.createRequest(self, operation, request, data)
def __init__(self, parent, cache_size = 100, cache_dir='.webkit_cache'): super(NetWorkAccessManager, self).__init__(parent) self.finished.connect(self._finished) cache = QNetworkDiskCache() cache.setCacheDirectory(cache_dir) cache.setMaximumCacheSize(cache_size * 1024 * 1024) # need to convert cache value to bytes self.setCache(cache)
def __init__(self, *args): QNetworkAccessManager.__init__(self, *args) self.current_root = None self.cache = QNetworkDiskCache(self) self.setCache(self.cache) self.cache.setCacheDirectory( PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(0)
def close(self): self.stop() self.blank() self.stop() self.nam.setCache(QNetworkDiskCache()) self.nam.cache = None self.nam = self.page = None
def __init__(self, *args): QNetworkAccessManager.__init__(self, *args) self.current_root = None self.cache = QNetworkDiskCache(self) self.setCache(self.cache) self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(0)
def __init__(self, log, disk_cache_size=50, parent=None): QNetworkAccessManager.__init__(self, parent) self.reply_count = 0 self.log = log if disk_cache_size > 0: self.cache = QNetworkDiskCache(self) self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(int(disk_cache_size * 1024 * 1024)) self.setCache(self.cache) self.sslErrors.connect(self.on_ssl_errors) self.pf = ProxyFactory(log) self.setProxyFactory(self.pf) self.finished.connect(self.on_finished) self.cookie_jar = QNetworkCookieJar() self.setCookieJar(self.cookie_jar) self.main_thread = current_thread() self.report_reply_signal.connect(self.report_reply, type=Qt.QueuedConnection)
def __init__(self, parent, cache_size=100, cache_dir='.webkit_cache'): super(NetWorkAccessManager, self).__init__(parent) self.finished.connect(self._finished) cache = QNetworkDiskCache() cache.setCacheDirectory(cache_dir) cache.setMaximumCacheSize(cache_size * 1024 * 1024) # need to convert cache value to bytes self.setCache(cache)
class NetworkAccessManager(QNetworkAccessManager): OPERATION_NAMES = { getattr(QNetworkAccessManager, '%sOperation' % x): x.upper() for x in ('Head', 'Get', 'Put', 'Post', 'Delete', 'Custom') } def __init__(self, *args): QNetworkAccessManager.__init__(self, *args) self.current_root = None self.cache = QNetworkDiskCache(self) self.setCache(self.cache) self.cache.setCacheDirectory( PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(0) def createRequest(self, operation, request, data): url = unicode(request.url().toString(QUrl.None)) if operation == self.GetOperation and url.startswith('file://'): path = url[7:] if iswindows and path.startswith('/'): path = path[1:] c = current_container() try: name = c.abspath_to_name(path, root=self.current_root) except ValueError: # Happens on windows with absolute paths on different drives name = None if c.has_name(name): try: return NetworkReply( self, request, c.mime_map.get(name, 'application/octet-stream'), name) except Exception: import traceback traceback.print_exc() return QNetworkAccessManager.createRequest(self, operation, request, data)
class NetworkAccessManager(QNetworkAccessManager): # {{{ OPERATION_NAMES = {getattr(QNetworkAccessManager, '%sOperation'%x) : x.upper() for x in ('Head', 'Get', 'Put', 'Post', 'Delete', 'Custom') } report_reply_signal = pyqtSignal(object) def __init__(self, log, disk_cache_size=50, parent=None): QNetworkAccessManager.__init__(self, parent) self.reply_count = 0 self.log = log if disk_cache_size > 0: self.cache = QNetworkDiskCache(self) self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_')) self.cache.setMaximumCacheSize(int(disk_cache_size * 1024 * 1024)) self.setCache(self.cache) self.sslErrors.connect(self.on_ssl_errors) self.pf = ProxyFactory(log) self.setProxyFactory(self.pf) self.finished.connect(self.on_finished) self.cookie_jar = QNetworkCookieJar() self.setCookieJar(self.cookie_jar) self.main_thread = current_thread() self.report_reply_signal.connect(self.report_reply, type=Qt.QueuedConnection) def on_ssl_errors(self, reply, errors): reply.ignoreSslErrors() def createRequest(self, operation, request, data): url = unicode(request.url().toString(QUrl.None)) operation_name = self.OPERATION_NAMES[operation] debug = [] debug.append(('Request: %s %s' % (operation_name, url))) for h in request.rawHeaderList(): try: d = ' %s: %s' % (h, request.rawHeader(h)) except: d = ' %r: %r' % (h, request.rawHeader(h)) debug.append(d) if data is not None: raw = data.peek(1024) try: raw = raw.decode('utf-8') except: raw = repr(raw) debug.append(' Request data: %s'%raw) self.log.debug('\n'.join(debug)) return QNetworkAccessManager.createRequest(self, operation, request, data) def on_finished(self, reply): if current_thread() is not self.main_thread: # This method was called in a thread created by Qt. The python # interpreter may not be in a safe state, so dont do anything # more. This signal is queued which means the reply wont be # reported unless someone spins the event loop. So far, I have only # seen this happen when doing Ctrl+C in the console. self.report_reply_signal.emit(reply) else: self.report_reply(reply) def report_reply(self, reply): reply_url = unicode(reply.url().toString(QUrl.None)) self.reply_count += 1 err = reply.error() if err: l = self.log.debug if err == reply.OperationCanceledError else self.log.warn l("Reply error: %s - %d (%s)" % (reply_url, err, unicode(reply.errorString()))) else: debug = [] debug.append("Reply successful: %s" % reply_url) for h in reply.rawHeaderList(): try: d = ' %s: %s' % (h, reply.rawHeader(h)) except: d = ' %r: %r' % (h, reply.rawHeader(h)) debug.append(d) self.log.debug('\n'.join(debug)) def py_cookies(self): for c in self.cookie_jar.allCookies(): name, value = map(bytes, (c.name(), c.value())) domain = bytes(c.domain()) initial_dot = domain_specified = domain.startswith(b'.') secure = bool(c.isSecure()) path = unicode(c.path()).strip().encode('utf-8') expires = c.expirationDate() is_session_cookie = False if expires.isValid(): expires = expires.toTime_t() else: expires = None is_session_cookie = True path_specified = True if not path: path = b'/' path_specified = False c = Cookie(0, # version name, value, None, # port False, # port specified domain, domain_specified, initial_dot, path, path_specified, secure, expires, is_session_cookie, None, # Comment None, # Comment URL {} # rest ) yield c
def create_favicon_cache(): c = QNetworkDiskCache() c.setCacheDirectory(os.path.join(cache_dir, 'favicons')) c.setMaximumCacheSize(25 * 1024 * 1024) return c