Entry
Is there a simple script somewhere I can look at that sends and retrieves cookies via CGI?
May 25th, 2000 07:35
unknown unknown, Oleg Broytmann, Michal Wallace
# ----- create -----
#! /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["id"] = str(1024)
cookie["id"]["expires"] = time.strftime("%a, %d-%b-%Y %T GMT", t)
cookie["id"]["path"] = "/"
print cookie
try:
print "Content-type: text/html"
set_cookie()
print "Location: test_c.py"
except:
#print exception
# ----- test -----
#! /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 exception
If you use weblib (http://weblib.sourceforge.net/ - get the cvs version,
not the snapshot):
-----------------------
import weblib
## show cookies from the browser:
print "the cookies are:"
print "<ul>"
for c in weblib.request.cookie.keys():
print "<li><b>%s</b> - %s</li>", (c, weblib.request.cookie[c])
print "</ul>"
## set some new cookies:
weblib.response.setCookie("x", "this is x")
weblib.response.setCookie("y", "this is y")