n00b with urllib2: How to make it handle cookie automatically?

7stud bbxx789_05ss at yahoo.com
Fri Feb 22 22:31:39 CET 2008

On Feb 21, 11:50 pm, est <electronix... at gmail.com> wrote:
> Hi all,
> I need urllib2 do perform series of HTTP requests with cookie from
> PREVIOUS request(like our browsers usually do ). Many people suggest I
> use some library(e.g. pycURL) instead but I guess it's good practise
> for a python beginner to DIY something rather than use existing tools.
> So my problem is how to expand the urllib2 class
> from cookielib import CookieJar
> class SmartRequest():
>     cj=CookieJar()
>     def __init__(self, strUrl, strContent=None):
>         self.Request    =   urllib2.Request(strUrl, strContent)
>         self.cj.add_cookie_header(self.Request)
>         self.Response   =   urllib2.urlopen(Request)
>         self.cj.extract_cookies(self.Response, self.Request)
>     def url
>     def read(self, intCount):
>         return self.Response.read(intCount)
>     def headers(self, strHeaderName):
>         return self.Response.headers[strHeaderName]
> The code does not work because each time SmartRequest is initiated,
> object 'cj' is cleared.

That's because every time you create a SmartRequest, this line


That creates a new, *empty* cookie jar, i.e. it has no knowledge of
any previously set cookies.

> How to avoid that?

If you read the docs on the cookielib module, and in particular
CookieJar objects, you will notice that CookieJar objects are
described in a section that is titled:  CookieJar and FileCookieJar

Hmm...I wonder what the difference is between a CookieJar object and a
FileCookieJar Object?

FileCookieJar implements the following additional methods:

save(filename=None, ignore_discard=False, ignore_expires=False)
Save cookies to a file.

load(filename=None, ignore_discard=False, ignore_expires=False)
Load cookies from a file.

That seems promising.

More information about the Python-list mailing list