Python Cookies module? Bugs?
Oleg Broytmann
phd at phd.russ.ru
Fri Oct 22 11:31:00 EDT 1999
On Fri, 22 Oct 1999, Benjamin Schollnick wrote:
> I'm forced to do this:
>
> C:\>python
>
> >>> mycookie["PSG"]["path"] = "/docushare"
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "D:\OS2\PYTHON\LIB\UserDict.py", line 14, in __getitem__
> def __getitem__(self, key): return self.data[key]
> KeyError: PSG
>
> ^^^ Error ^^^^
>
> >>> mycookie["PSG"] = "PSG Cookie"
> >>> mycookie["PSG"]["path"]="/docushare"
> >>> print mycookie
> Set-Cookie: PSG="PSG Cookie"; Path=/docushare;
>
> Can anyone confirm that this is proper behavior for this
> module?
But of course. It is normal to Python. Python (nont Cookie.py) saw
mycookie["PSG"] in expression mycookie["PSG"]["path"] and tryed to find the
object. But the object isn't there.
You certainly must add an object mycookie["PSG"] before setting its
properties.
> Also, in my informal testing, when I start messing with
> Cookie Attributes (i.e. Path, etc), the cookie isn't
> being restored properly...
>
> Anyone have any hints, and/or examples of Cookie.py use?
----- create cookie -----
#! /usr/local/bin/python -O
def set_cookie():
import time
t = time.gmtime(time.time())
t = (t[0] + 10,) + t[1:] # Add 10 years!
import Cookie
cookie = Cookie.Cookie()
cookie["anketa_id"] = str(1024)
cookie["anketa_id"]["expires"] = time.strftime("%a, %d-%b-%Y %T GMT", t)
cookie["anketa_id"]["path"] = "/"
print cookie
try:
print "Content-type: text/html"
set_cookie()
print "Location: test_c.py"
except:
print "Error..."
----- /create cookie -----
----- test cookie -----
#! /usr/local/bin/python -O
def get_cookie():
import os, Cookie
cookie = Cookie.Cookie(os.environ.get("HTTP_COOKIE"))
c = cookie.output("Cookie: ")
if c:
print c
else:
print "Cookies not found"
try:
print "Content-type: text/html"
print
get_cookie()
except:
print "Error..."
----- /test cookie -----
> Is there any "preferences" in the python world on Cookie
> Modules? (I.e. any better/worse/etc modules)
It s excellent module, IMHO! Just use right way...
Oleg.
----
Oleg Broytmann Foundation for Effective Policies phd at phd.russ.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-list
mailing list