Python Requests and persistent sessions

You can easily create a persistent session using:

s = requests.Session()

After that, continue with your requests as you would:

s.post('https://localhost/login.py', login_data)
# logged in! cookies saved for future requests.
r2 = s.get('https://localhost/profile_data.json', ...)
# cookies sent automatically!
# do whatever, s will keep your cookies intact :)

For more about Sessions: https://docs.python-requests.org/en/latest/user/advanced/#session-objects

Leave a Comment