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

est electronixtar at gmail.com
Fri Feb 22 07:50:49 CET 2008


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. How to avoid that?
The only stupid solution I figured out is use a global CookieJar
object. Is there anyway that could handle all this INSIDE the class?

I am totally new to OOP & python programming, so could anyone give me
some suggestions? Thanks in advance



More information about the Python-list mailing list