Simple Cookie Script: Not recognising Cookie

Jimbo nilly16 at
Thu Apr 8 07:03:22 CEST 2010

Hi I have a simple Python program that assigns a cookie to a web user
when they open the script the 1st time(in an internet browser). If
they open the script a second time the script should display the line
" You have been here 2 times." , if they open the script agai it
should show on the webpage "You have been here 3 times" and so on.

But for some reason, my program is not assigning or recognising an
assigned cookie & outputing the line "You have been here x times". I
have gone over my code for like 2 hours now I cant figure out what is
going wrong??

Can you help me figure out whats wrong? I have my own cgi server that
just runs on my machine so its not that its the code to recognise/
assign a cookie

[code]#!/usr/bin/env python

import Cookie
import cgi
import os

HTML_template = """

    <p> %s </p>

def main():

    # Web Client is new to the site so we need to assign a cookie to
    cookie = Cookie.SimpleCookie()
    cookie['SESSIONID'] = '1'
    code = "No cookie exists. Welcome, this is your first visit."

    if 'HTTP_COOKIE' in os.environ:
        cookie = Cookie.SimpleCookie(os.environ['HTTP_COOKIE'])
        # If Web client has been here before
        if cookie.has_key('SESSIONID'):
            cookie['SESSIONID'].value = int(cookie['SESSIONID'].value)
            code = "You have been here %s times." %
            cookie = Cookie.SimpleCookie()
            cookie['SESSIONID'] = '1'
            code = "I Have a cookie, but SESSIONID does not exist"

    print "Content-Type: text/html\n"
    print HTML_template % code

if __name__ == "__main__":

More information about the Python-list mailing list