Requests: Save and load session

Created on 2 Apr 2015  ·  3Comments  ·  Source: psf/requests

I'd like to be able to save and load a session to disk. I think this means just saving and loading the cookies, but perhaps there are other nuances that would need to be included.

I'm interested in using Robobrowser (https://github.com/jmcarp/robobrowser) which wraps requests. I'd like to be able to persist the browser between running my application, which means being able to save and load the session to disk.

There is a SO post regarding how to do this. I think tellingly, the most popular answer is wrong.
http://stackoverflow.com/questions/13030095/how-to-save-requests-python-cookies-to-a-file

Thanks,
Jim

Most helpful comment

The easiest thing to do is just to pickle the whole session object:

import requests, requests.utils, pickle
session = requests.session()
# Make some calls
with open('somefile', 'w') as f:
    pickle.dump(session, f)
with open('somefile') as f:
    session = pickle.load(f)

All 3 comments

The easiest thing to do is just to pickle the whole session object:

import requests, requests.utils, pickle
session = requests.session()
# Make some calls
with open('somefile', 'w') as f:
    pickle.dump(session, f)
with open('somefile') as f:
    session = pickle.load(f)

As an addendum: looks like Python3 expects the 'b' flag during write and read:

with open('somefile', 'wb') as f:
    pickle.dump(session, f)
with open('somefile', 'rb') as f:
    session = pickle.load(f)

I tried using this approach, however I could not get TLS session resumption to work after reloading the pickled session. Does anyone know how to achieve this behavior?

Was this page helpful?
0 / 5 - 0 ratings