Cookie based authentication

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


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

> I am still stuck on how to use client cookie
> (http://wwwsearch.sourceforge.net/ClientCookie/). 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 http://www.fool.com/login.asp
> (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("http://www.fool.com/Login.asp")
> result = urllib2.urlopen(request)
> c = ClientCookie.Cookies()
> c.extract_cookies(result, request)
[...]
>
c.set_cookie(None,"Wookie=Ref=","FoolV=5&Uid=123146592&LastVisit=6%2F17%2F20
>
02+1%3A24%3A20+PM&FirstVisit=6%2F17%2F2002+12%3A27%3A22+PM&Username=westlab"
> ,"/","fool.com/",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("http://www.fool.com/Login.asp")

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?


John




More information about the Python-list mailing list