Reading cookies on the client side

Charles Anderson cander at ieor.berkeley.edu
Thu Jul 13 20:49:15 EDT 2000


I was trying to figure out how to write a robot of sorts to get some
info from a site that uses cookies.  I poked around, and there didn't
seem to be anything existing in Python to handle it.  So, I wrote an
extension of FancyURLopen.

The biggest problem I had to deal with was the site I was connecting to
returned some cookies then did a redirect to another page that returned
more cookies.  All of the cookies are needed for subsequent
interactions.  So, this class overrides the handler for 302 redirects.

Caveat: I didn't know Jack about cookies or HTTP when I started this.
Therefore, this may be completely wacked, but it works for me.

enjoy,
Charles.
------

from urllib import FancyURLopener
import string

class CookieURLopener(FancyURLopener):
    def __init__(self):
        FancyURLopener.__init__(self)
        self.cookies = []

    def open_http(self, url, data=None):
        """Handle an HTTP open request.  We pass this to FancyURLopener
to do
           the real work.  Afterwards, we scan the info() for
cookies."""
        result = FancyURLopener.open_http(self, url, data)
        self.eatCookies(result.info())
        return result

    def http_error_302(self, url, fp, errcode, errmsg, headers,
data=None):
        """Handle an HTTP redirect.  First we get the cookies from the
headers
           off of the initial URL.  Then hand it off to the super-class,
which
           will call back into our open_http method, where we can pick
up
           more cookies."""
        self.eatCookies(headers)
        result = FancyURLopener.http_error_302(self, url, fp, errcode,
errmsg, headers, data=None)
        return result

    def eatCookies(self, headers):
        """Scan a set of response headers for cookies.  We add each
cookie to
           our list."""
        cookies = headers.getallmatchingheaders('set-cookie')
        for c in cookies:
            self.addCookie(string.strip(c[12:]))        # "set-cookie: "
is 11 characters

    def addCookie(self, cookie):
        """Add a cookie to our cache of them and call addheaders of our
parent."""
        self.cookies.append(cookie)
        self.addheader('Cookie', cookie)

    def dumpCookies(self):
        for c in self.cookies:
            print 'cookie:', c





More information about the Python-list mailing list