2

I need to accomplish a login task in my own project.Luckily I found someone has it done already.
Here is the related code.

import re,urllib,urllib2,cookielib

class Login():   
    cj = cookielib.LWPCookieJar()     
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  

def __init__(self,name='',password='',domain=''):  
    self.name=name  
    self.password=password  
    self.domain=domain  
    urllib2.install_opener(self.opener)  

def login(self):  
    params = {'domain':self.domain,'email':self.name,'password':self.password}  
    req = urllib2.Request(  
        website_url,  
        urllib.urlencode(params)  
    )  

    self.openrate = self.opener.open(req)  

    print self.openrate.geturl()  
    info = self.openrate.read()  

I've tested the code, it works great (according to info).
Now I want to port it to Python 3 as well as using requests lib instead of urllib2.
My thoughts:

  1. since the original code use opener, though not sure, I think its equivalent in requests is requests.Session
  2. Am I supposed to pass in a jar = cookiejar.CookieJar() when making request? Not sure either.

I've tried something like

import requests
from http import cookiejar
from urllib.parse import urlencode

jar = cookiejar.CookieJar()
s = requests.Session()
s.post(  
    website_url,  
    data = urlencode(params),
    allow_redirects = True,
    cookies = jar  
) 

Also, followed the answer in Putting a `Cookie` in a `CookieJar`, I tried making the same request again, but none of these worked.

That's why I'm here for help.
Will someone show me the right way to do this job? Thank you~

Community
  • 1
  • 1
laike9m
  • 14,908
  • 16
  • 92
  • 123

1 Answers1

5
  1. An opener and a Session are not entirely analogous, but for your particular use-case they match perfectly.
  2. You do not need to pass a CookieJar when using a Session: Requests will automatically create one, attach it to the Session, and then persist the cookies to the Session for you.
  3. You don't need to urlencode the data: requests will do that for you.
  4. allow_redirects is True by default, you don't need to pass that parameter.

Putting all of that together, your code should look like this:

import requests

s = requests.Session()
s.post(website_url, data = params)

Any future requests made using the Session you just created will automatically have cookies applied to them if they are appropriate.

Lukasa
  • 11,534
  • 4
  • 27
  • 32
  • Thank you, I finally got it to work.It's having `urlencode` that caused the problem.BTW, you said "An opener and a Session are not entirely analogous", then what is the equivalent of opener? – laike9m Dec 20 '13 at 12:40
  • There is no equivalent of opener, because Requests rejects the idea entirely. `Session`s are where Requests maintains state, so in that sense they _are_ like openers, but there's only ever one kind and it does all state maintenance: in that sense they are _unlike_ openers. – Lukasa Dec 20 '13 at 14:46