[Tutor] Cookies
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Apr 9 15:39:41 EDT 2004
On Thu, 8 Apr 2004, Scott wrote:
> Hi all. Is there a way to get the value of a particular cookie for a
> particular website that has been left behind by a web-browser?
> Hopefully, this would work whether the user has Mozilla or IE or Opera,
> etc. Thanks.
Hi Scott,
Hmmm... I believe that cookies are stored on by the browser itself, so if
you have access to the cookie file that lives on the browser, it should be
possible to eat that.
The Mozilla browser, for example, keeps a 'cookies.txt' file, and it looks
something like this:
###
[dyoo at tesuque 987rqvz4.slt]$ head cookies.txt
# HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
# To delete cookies, use the Cookie Manager.
ads.specificpop.com FALSE FALSE 1609459199 id
526702577
ads.specificpop.com FALSE FALSE 1609459199 fci
10-326-1-1060739521-1%3A10-35-1-1061334715-1
www.baxter2.com FALSE FALSE 1092257148 lang english
www.linuxjournal.com FALSE FALSE 1092862778 lang
english
.specificpop.com TRUE FALSE 1609459199 cpa
156-1136-109-1061334715
###
Hmmm... so it should be possible to parse this file, since it's
tab-delimited. Ah! Someone has written a parser for cookie files in the
Python Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66454
Ok, so that step is done. *grin*
I did some more Googling, and according to:
http://www.computer-2tr.com/Tips/03/20030310.html
the cookies for IE lives in a 'Cookies' subdirectory, but that
subdirectory is in several possible places, depending on what version of
Windows you're running.
Thankfully, there's also an entry in the Cookbook to grab IE cookie files
without having to worry about that detail:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/80443
I can't test this since I don't have Windows available at the moment.
Can anyone confirm that it has the same structure as the Mozilla cookie
file?
So all the pieces are almost there. I don't think anyone has yet worked
on writing a nice set of functions to uniformly grab cookie files for all
the different browsers, though. If one did exist, I'd expect it in
'webbrowser', so perhaps it might be worthwhile to try adding some
functionality in 'webbrowser' to do this.
Talk to you later!
More information about the Tutor
mailing list