Added functions to save and load session cookies to and from file
This uses some fairly unknown functions from the "requests" library
This commit is contained in:
@@ -189,6 +189,36 @@ class Client(object):
|
|||||||
payload=self._generatePayload(None)
|
payload=self._generatePayload(None)
|
||||||
return self._session.post(url, data=payload, timeout=timeout, files=files)
|
return self._session.post(url, data=payload, timeout=timeout, files=files)
|
||||||
|
|
||||||
|
def saveSession(self, sessionfile):
|
||||||
|
"""Dumps the session cookies to (sessionfile).
|
||||||
|
WILL OVERWRITE ANY EXISTING FILE
|
||||||
|
|
||||||
|
:param sessionfile: location of saved session file
|
||||||
|
"""
|
||||||
|
|
||||||
|
log.info('Saving session')
|
||||||
|
with open(sessionfile, 'w') as f:
|
||||||
|
# Grab cookies from current session, and save them as JSON
|
||||||
|
f.write(json.dumps(self._session.cookies.get_dict(), ensure_ascii=False))
|
||||||
|
|
||||||
|
def loadSession(self, sessionfile):
|
||||||
|
"""Loads session cookies from (sessionfile)
|
||||||
|
|
||||||
|
:param sessionfile: location of saved session file
|
||||||
|
"""
|
||||||
|
|
||||||
|
log.info('Loading session')
|
||||||
|
with open(sessionfile, 'r') as f:
|
||||||
|
try:
|
||||||
|
j = json.load(f)
|
||||||
|
if not j or 'c_user' not in j:
|
||||||
|
return False
|
||||||
|
# Load cookies into current session
|
||||||
|
self._session.cookies = requests.cookies.merge_cookies(self._session.cookies, j)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception('Invalid json in {}, or bad merging of cookies'.format(sessionfile))
|
||||||
|
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
if not (self.email and self.password):
|
if not (self.email and self.password):
|
||||||
|
Reference in New Issue
Block a user