Created
September 18, 2013 18:38
-
-
Save rca/6613537 to your computer and use it in GitHub Desktop.
Persist a requests session between uses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pickle | |
import requests | |
URL = 'http://foo.com' | |
# create a session object | |
session = requests.Session() | |
# configure the session (optional) | |
session.auth = ('user', 'pass') | |
print 'auth before pickle: {}'.format(session.auth) | |
# use the session to make requests | |
response = session.get(URL) | |
# drop the session into a pickle object | |
session_pickle = pickle.dumps(session) | |
## | |
# do what you need to do to persit it somewhere | |
# e.g. request.session['requests_session'] = session_pickle | |
## | |
# when you need to use it again, depickle the object | |
## | |
# do what you need to do to get it from storage | |
# e.g. session_pickle = request.session['requests_session'] | |
## | |
session = pickle.loads(session_pickle) | |
print 'auth after pickle: {}'.format(session.auth) | |
response = session.get(URL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment