Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and load session #2522

Closed
JimHokanson opened this issue Apr 2, 2015 · 3 comments
Closed

Save and load session #2522

JimHokanson opened this issue Apr 2, 2015 · 3 comments

Comments

@JimHokanson
Copy link

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

@Lukasa
Copy link
Member

Lukasa commented Apr 2, 2015

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)

@Lukasa Lukasa closed this as completed Apr 2, 2015
@brunobelotti
Copy link

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)

@jsmedmar
Copy link

jsmedmar commented Oct 8, 2020

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?

@psf psf locked as resolved and limited conversation to collaborators Oct 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants