Python CookieJar saves cookie but does not send it to the site

I am trying to login using urllib2 and cookiejar. It saves the session identifier, but when I try to open another link that requires authentication, it says that I have not logged in. What am I doing wrong?

Here is the code that is not suitable for me:

import urllib import urllib2 import cookielib cookieJar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar)) # Gives response saying that I logged in succesfully response = opener.open("http://site.com/login", "username=testuser&password=" + md5encode("testpassword")) # Gives response saying that I am not logged in response1 = opener.open("http://site.com/check") 
+4
source share
2 answers

Your implementation seems beautiful ... and should work.

It should be sent in the correct cookies , but I see it as a case when the site does not actually register you.

How can you say that it does not send cookies or there may be cookies that you receive, not the one that checks you.

Use: response.info() to view the response headers to see which cookies you actually receive.

The site cannot register you because:

  • He has a check on the User-agent , which you do not install, since some sites open from 4 main browsers only to restrict access to the bot.

  • The site may look for some special hidden form field that you cannot submit.

1 tip:

 from urllib import urlencode # Use urlencode to encode your data data = urlencode(dict(username='testuser', password=md5encode("testpassword"))) response = opener.open("http://site.com/login", data) 

Moreover, it is strange here:

  • You pass the md5 password before sending. (Strange)
  • This is usually done by the server before comparing with the database.
  • This is only possible if site.com implements md5 in javascript.
  • This is a very rare case, as it can only be 0.01% of websites.
  • Verify that this can be a problem and you provide a hashed form, not an actual password for the server.
  • So, the server will compute md5 again for your md5 hash.

Departure..!! :)

+4
source

I had a similar problem with my own test server, which worked fine with the browser, but not with the urllib2.build_opener solution.

The problem seems to be in urllib2. As these answers say, it's easier to use a more powerful mechanize library instead of urllib2:

 cookieJar = cookielib.CookieJar() browser = mechanize.Browser() browser.set_cookiejar(cookieJar) opener = mechanize.build_opener(*browser.handlers) 

And the opener will work as expected!

+2
source

Source: https://habr.com/ru/post/1382801/


All Articles