Ruben Roy
Ruben Roy

Reputation: 617

Python: Cookies not persisting between requests

I am using this code to login to a website. After the POST request, the website redirects to the profile page and I can see that I have logged in. But the next request does not keep me logged in, even if I store the cookies. This is not in particular about the pastebin website, so please dont tell me to use their API

def web_login(username,password):
        LOGIN_URL = 'http://pastebin.com/login.php'
        HOME_URL = 'http://pastebin.com/'
        jar = cookielib.CookieJar()
        payload = {"user_name":username,"user_password":password,"submit_hidden":"submit_hidden"}
        s = requests.Session()
        user_agent = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0'}
        r = s.post(LOGIN_URL,data=payload,headers=user_agent,cookies=jar)
        r = s.get(HOME_URL,headers=user_agent,cookies=jar)        
        print r.text

Upvotes: 4

Views: 2036

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28747

As @falsetru pointed out your issue is using an external cookie jar. The reason is that each Session object has its own cookie jar and will store them automatically for you. Passing in a value to cookies tells the Session to prefer that jar over its internal one and will not update the one passed in. If you wanted to be more explicit you could do this:

r = s.post(LOGIN_URL, data=payload, headers=user_agent)
jar = r.cookies
home = s.get(HOME_URL, headers=user_agent, cookies=jar)

Another thing to note is that if you're sending those same headers each time you can do:

s = requests.Session()
s.headers.update(user_agent)
r = s.post(LOGIN_URL, data=payload)
home = s.get(HOME_URL)

I hope this helps you understand why what @falsetru told you works and helps others who come along and see this as well.

I should also point out that if you wanted to use a custom cookie jar that provides the same interface as cookielib.CookieJar you can also do:

jar = MyCookieJar()
s = requests.Session()
s.cookies = jar
r = s.post(LOGIN_URL, data=payload)
home = s.get(HOME_URL)

And then you can access your cookies via both jar and s.cookies.

Upvotes: 4

Related Questions