Cookies / CGI

Oleg Broytmann phd at phd.russ.ru
Wed May 24 07:15:23 EDT 2000


On Wed, 24 May 2000, RS Solutions wrote:
> Is there a simple script somewhere I can look at that sends and retrieves
> cookies via CGI?
> 
> I've looked at the cookie module but its not clear to me how that gets
> posted to the end-user.

# ----- 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

Oleg.            (All opinions are mine and not of my employer)
---- 
    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