Cookie based authentication

John J. Lee jjl at
Thu Jun 27 11:59:20 CEST 2002

On 2002-06-17 11:32:44 PST, Chandan Mathur <cmathur at> wrote:

> I am still stuck on how to use client cookie
> ( I am trying to
> write a

As I said last time, you'd be more likely to get a timely answer by
mailing me direct.

> script that automatically logs in to
> (SSL encryption) and downloads pages that i need. This is what i
> have tried so far: I logged in using internet explorer and opened
> the cookie file from windows explorer. Using set_cookie method in
> Cookies, i tried to set the cookie as best as i could.. (i probably
> did something wrong here).
> import ClientCookie
> import urllib2
> import urllib
> request = urllib2.Request("")
> result = urllib2.urlopen(request)
> c = ClientCookie.Cookies()
> c.extract_cookies(result, request)
> ,"/","",None,None,None,None,None,None)

You're mixing two or three incompatible modes of operation here.  If
you want to do extraction and adding of cookie headers explicitly, use
the extract_cookies and add_cookie_header methods.  Otherwise, just
use urlopen:

import ClientCookie
result = ClientCookie.urlopen("")

Note that this is the urlopen from ClientCookie, not urllib2.  If you
use the urlopen from urllib2, it'll do any redirections (without any
cookie handling) before you even get to hear anything back from it at
all.  set_cookie is another level down further still.

All of this is in the docs...

> it gets the wrong page

'it gets the wrong page' is not all that helpful a description.  Could
you be a little more discursive next time?


More information about the Python-list mailing list