I used QNetworkAccessManager to load pages and login to some sites. I want to save cookies and load them to next using this programm.
I write this code to save cookies:
import shelve
self.netManager=QNetworkAccessManager()
#... Load Pages and Login ....
with shelve.open('LoginDb','c') as db:
db['cooki']=netManager.CookieJar()
and this code to load cookies:
with shelve.open('LoginDb','c') as db:
self.netManager.setCookieJar(db['cooki'])
But setCookieJar doesn't work and arise this error:
super-class init() of type QNetworkCookieJar was never called
What can i do to do this?
That error message typically means you subclassed QNetworkCookieJar
and forgot to call the superclass __init__
. You should do something like this (assuming Python 3):
class CookieJar(QNetworkCookieJar):
def __init__(self, parent):
super().__init__(parent) # You probably don't do this
# Do custom stuff here
I'd also advise against using shelve
. It's brittle, slow, and in certain
scenarios a security risk. I personally just store them in a plaintext file in
my project - something like this:
class CookieJar(QNetworkCookieJar):
# [...]
def parse_cookies(self):
cookies = []
with open('cookies', 'r') as f:
for line in f:
cookies += QNetworkCookie.parseCookies(line)
self.setAllCookies(cookies)
def save(self):
with open('cookies', 'w') as f:
for cookie in self.allCookies():
if not cookie.isSessionCookie():
f.write(cookie.toRawForm() + '\n')