[Tutor] Cookielib - CookieJar objects

D. Hartley denise.hartley at gmail.com
Wed Jun 29 20:16:34 CEST 2005


first attempt - myjar has no attribute 'value'

second attempt - 

re:

myjar = cookielib.CookieJar()
for cookie in myjar:
    print cookie.value

In this case the above code should print a single 'B'.


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,
because when I'm done with that function, I do:

jar6 = urlsearch(2)
for cookie in jar6:
    print cookie.value

(2 is number of times)

If I do urlsearch(1), I get B for the value (as expected).  If I do
urlsearch(2), I get Z (and only Z. Not BZ). Ditto urlsearch(3), I get
h (and only h, not BZh).

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?



> > Yes, whenever the documentation talks about something being "iterable",
> > they really mean that we can use a for loop across it.  

This seems perfectly rational to me.  However, having the
"cookie.value" part used in an actual example would have been very
helpful.


> > I wonder why; I haven't been able to figure out a good reason for this
> > kind of interface.  Are cookie jars files known to be large?

I assumed that a cookie jar would work kind of like a list. I could
add new cookies to it at each step of my while loop, and then iterate
over the "list" of cookies to get out the pieces of info I wanted. I'm
hoping this is correct, because that's how I *want* to use this cookie
jar!

> This only pushes the mystery one step back, though - the attribute of the Cookie that is recursively iterated is called 'item', but the Cookie class defined in cookielib has no attribute 'item'.

Yeah, it was kind of confusing why it was called an "item" in one
version and a "value" in another. But then, most things are confusing
to me ;)

> 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 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.

In any case, if anyone has any ideas why my cookiejar isn't collecting
a bunch of cookies but instead only returning the last one, I'd be
very grateful!

Thanks again for all your help and suggestions.

~Denise


More information about the Tutor mailing list