ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Cookielib - CookieJar objects
by Kent Johnson other posts by this author
Jun 30 2005 6:11AM messages near this date
Re: [Tutor] Cookielib - CookieJar objects | [Tutor] Minesweeper OOT
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 cooki
ejar 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 rel
ying 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\cookieli
b.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 writt
en in Python and the source is easily available in Python24\Lib (on Windows, the path is dif
ferent on Linux).

Kent

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
D. Hartley
Liam Clarke
Peter Szinek
Danny Yoo
Kent Johnson
D. Hartley
Kent Johnson

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved