There is no immediate way to do so, but it’s not hard to do.
You can get a CookieJar
object from the session with session.cookies
, and use pickle
to store it to a file.
A full example:
import requests, pickle
session = requests.session()
# Make some calls
with open('somefile', 'wb') as f:
pickle.dump(session.cookies, f)
Loading is then:
session = requests.session() # or an existing session
with open('somefile', 'rb') as f:
session.cookies.update(pickle.load(f))
The requests
library uses the requests.cookies.RequestsCookieJar()
subclass, which explicitly supports pickling and a dict-like API. The RequestsCookieJar.update()
method can be used to update an existing session cookie jar with the cookies loaded from the pickle file.