Struggling to retrieve csrf token from a cookiejar object

1.4k views Asked by At

I am using the code below based on this answer to store authentication cookies in a file. This allows me to avoid having to log in every time I run the program.

Now, fetching the csrftoken from regular Session cookies works well, but when I try to fetch it from the cookiejar using csrf = s.cookies['csrftoken'], I get

AttributeError: LWPCookieJar instance has no attribute '__getitem__'`

I understand that this happens because I treat s.cookies as a list even though is now a LWPCookieJar object. I suppose this leaves me with two options:

Either:

  • Extract the list from the LWPCookieJar object and then getting the csrf token is easy.
  • or, Find the appropriate syntax to retrieve the csrf token directly from the LWPCookieJar.

How can I do this?


import os
import requests
from cookielib import LWPCookieJar

s = requests.Session()
s.cookies = LWPCookieJar('cookiejar')
if os.path.exists('cookiejar'):
    # Load saved cookies from the file and use them in a request
    print('loading saved cookies')
    s.cookies.load()        
else:
    # Create a new cookies file and set our Session's cookies
    print('saving cookies')
    s.cookies.save()
r = s.get('http://httpbin.org/cookies')
print(r.text)
# Save the session's cookies back to the file
s.cookies.save()
1

There are 1 answers

0
octosquidopus On BEST ANSWER

I finally figured it out. Not sure if it can be further simplified.

import os, requests
from cookielib import LWPCookieJar

def login():
    global s
    s = requests.Session()
    if os.path.exists('cookiejar'):
        print('Cookies exist! Fast login.\r'),
        global csrf
        with open('cookiejar') as f:
            s.cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
        csrf = s.cookies['csrftoken']
        print('csrf: {0}').format(csrf) # Debug information
    else:
        print('Cookies do not exist. Slow login.\r'),
        s.get('http://www.example.com/') # to get csrf
        csrf = s.cookies['csrftoken']
        data= dict(csrftoken=csrf, username="user", password="pass")
        s.post('https://www.example.com/login/', data=data, headers=headers)
        with open('cookiejar', 'w') as f:
            pickle.dump(requests.utils.dict_from_cookiejar(s.cookies), f)