ClientCookie bug (followup)

John J. Lee jjl at
Sat Aug 16 12:33:13 CEST 2003

cartermark46 at (Mark Carter) writes:
> John: Why without the delayload argument?
> My response:
> The following code:
> import ClientCookie
> c = ClientCookie.MSIECookieJar(delayload=1)
> c.load_cookie_data("hemscott-cookie.bin")
> import urllib2
> url = ''
> request = urllib2.Request(url)
> response = urllib2.urlopen(request)
> request2 = urllib2.Request(url)
> c.add_cookie_header(request2)
> response2 = urllib2.urlopen(request2)
> print response2.geturl()
> print  # headers
> for line in response2.readlines():  # body
>        print line

Take note of this comment from the web page:

| # Don't copy this blindly!  You probably want to follow the examples
| # above, not this one.

No matter how many times and how many places I say this, everybody
seems to be driven to make the same mistakes.  I guess people don't
believe it's as simple as calling urlopen, though I explictly say it
*is* that simple in most cases in the second sentence of the

DON'T USE *BOTH* add_cookie_header/extract_cookies *AND* urlopen.
urlopen is all you need, unless you're not using urllib2.  Probably
won't do any actual harm, but it's completely pointless and
obfuscatory.  You're also failing to use ClientCookie.urlopen (which
unlike urllib2.urlopen, knows about cookies), because you blindly
copied the third example from the web page.  You're also using Request
objects for no apparent reason, and you're attempting to fetch the
same url twice, again because you're blindly copying.  Sigh -- if
people have to blindly copy, why not copy from the code I actually
*tell* people to copy from, instead of the code I explictly tell
people *not* to copy from?  Ngghhh!  :-)

You want something like this:

import ClientCookie
c = ClientCookie.MSIECookieJar(delayload=1)
url = ''
response = ClientCookie.urlopen(url)


> produces the error:
> Traceback (most recent call last):
> whereas running it without delayload=1 causes it to run successfully.

Anyway, you have found another bug, so all is forgiven.

(the MSIE delayload feature is still a bit of a mess -- luckily, I can
pin the blame on the original author of that code, since it's pretty
directly ported from Perl ;)


More information about the Python-list mailing list