QNetworkDiskCache не подходит, т.к. не сохраняет ресурсы у которых установлен запрет кэширования.
PyQt: 4.8.6
OS: WinXP 32bit
Python: 2.7
Код
# coding: utf-8
try:
from hashlib import md5, sha1
except:
import md5, sha1
import os,sys
from PyQt4.QtNetwork import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def l(msg):
print >> sys.stderr, msg
class MyCache1(QAbstractNetworkCache):
def __init__(self, basedir, uid=None):
QAbstractNetworkCache.__init__(self)
self.basedir = basedir
if not os.path.exists(self.basedir):
os.mkdir(self.basedir)
self.uid = uid
def get_filename(self, url):
m = md5(url.toString()).hexdigest()
f = os.path.join(str(self.basedir), m)
return f + ".meta", f + ".data"
def cacheSize(self):
l("cacheSize")
return 100 * 1024 * 1024 # fake
def clear(self):
l("clear")
return 0
def data(self, url):
l("get data: " + url.toString())
mfn, dfn = self.get_filename(url)
try:
df = QFile(dfn)
df.open(QIODevice.ReadOnly)
return df
except:
l("get_data rturn 0")
return 0
def metaData(self, url):
l("metaData " + url.toString())
return QNetworkCacheMetaData()
def insert(self, device):
l("insert")
device.close()
def prepare(self, metaData):
l("get url: " + metaData.url().toString())
mfn, dfn = self.get_filename(metaData.url())
l("open file " + dfn)
df = QFile(dfn)
df.open(QIODevice.WriteOnly)
l("file opened " + dfn)
return df
def remove(self, url):
l("remove")
return True
def updateMetaData(self, metaData):
l("updateMetaData")
return
app = QApplication(sys.argv)
view = QWebView()
webpage = QWebPage()
view.setPage(webpage)
# устанавливаем свой кэш
c = MyCache1(os.path.join(os.getcwd(),"my_cache"))
webpage.networkAccessManager().setCache(c)
url ="http://qt.nokia.com/"
view.load(QUrl(url))
view.show()
app.exec_()
sys.exit()