Putting a `Cookie` in a `CookieJar`

Viewed 77221

I'm using the Python Requests library to make HTTP requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar with the cookie in it?

9 Answers

A Requests Session will receive and send cookies.

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'

(The code above is stolen from Session Objects.)

If you want cookies to persist on disk between runs of your code, you can directly use a CookieJar and save/load them:

from http.cookiejar import LWPCookieJar
import requests

cookie_file = '/tmp/cookies'
jar = LWPCookieJar(cookie_file)

# Load existing cookies (file might not yet exist)
try:
    jar.load()
except:
    pass

s = requests.Session()
s.cookies = jar

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

# Save cookies to disk, even session cookies
jar.save(ignore_discard=True)

Then look in the file /tmp/cookies:

#LWP-Cookies-2.0
Set-Cookie3: sessioncookie=123456789; path="/"; domain="httpbin.org"; path_spec; discard; version=0

A simplified version of overthink's answer, on how to get a cookiejar and persist the cookies in Python 3:

import requests

s = requests.Session()

r1 = s.get('https://stackoverflow.com')
print("r1", r1.cookies) # Have cookie
print("s", s.cookies)  # Have cookie(jar)

r2 = s.get('https://stackoverflow.com') #The cookie from r1 is resend
print("r2", r2.cookies) #No cookie (could be a new one)
print("s", s.cookies)  #Keep the cookie(jar) from r1

To persist the cookies between sessions you have to save and reuse the cookiejar in Session (the s variable).

If you get different answers between r1/r2/s on other sites, check if there is a redirect. As an example, r1/r2 will not get any cookie for https://www.stackoverflow.com, because it is redirected to the site without www.

Related