[Tutor] Cookielib - CookieJar objects

Kent Johnson kent37 at tds.net
Thu Jun 30 15:10:22 CEST 2005

D. Hartley wrote:
> This does work.  However, it only ever returns me one value.  For
> instance, the first three are BZh.  My code is:
> def urlsearch(times):
>     x = 12345
>     count = 0
>     myjar = cookielib.CookieJar()
>     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(myjar))
>     while count < times:
>         param = urllib.urlencode({'busynothing': x})
>         z = opener.open("http://www.pythonchallenge.com/pc/def/linkedlist.php?%s"
> % param)
>         text = z.read()
>         lister = string.split(text)
>         x = lister[-1]
>         count = count + 1
>     return myjar
> Now, if I'm using the jar the way I think I am, I'm creating it
> outside the while loop, and then when I use the opener within the
> while loop (to open each successive url), I should be adding a new
> cookie, right? so that I have a jar full of multiple cookies? But it's
> acting like it's just *replacing* the old cookie with the new one,

The *server* is reusing the same cookie for each URL, so it replaces the cookie in the cookiejar with a new cookie.

> Is it truly replacing the old cookie with the new one? That isn't what
> I want to do, and not how I would think a cookie jar would work.  Am I
> setting it up incorrectly?

Read the cookie data inside the loop and use a list to accumulate the values, instead of relying on the cookiejar to accumulate the values.

>>Take a look at cookielib.deepvalues() if you are curious.
> I'm looking at the cookielib page - why can't I find .deepvalues()?

It's an implementation detail - you have to look at the source code in Python24\Lib\cookielib.py
> It's funny to me, but some of these documentation pages I find exactly
> what I'm looking for, including examples, and can immediately put it
> to practical use in my code.  Some of the libraries I read a hundred
> times and can never seem to find what I'm looking for.

Sometimes it helps to look at the source code for the module. Many library modules are written in Python and the source is easily available in Python24\Lib (on Windows, the path is different on Linux).


More information about the Tutor mailing list