Cookie: Not understanding again

mosscliffe mcl.office at googlemail.com
Fri Jun 1 10:49:54 EDT 2007


I have the following code, which I thought would create a cookie, if
one did not exist and on the html form being sent to the server, it
would be availabe for interrogation, when the script is run a second
time.

It seems to me there is something basic missing in that I should need
to tell the server I am sending a cookie, but all the docs I have read
imply it is done automatically, if one creates a 'Cookie.SimpleCookie'

I know my understanding is poor of web server logic, but if anyone can
help, I will be most grateful.

This may be more to do with Web Server behaviour than python
programming, but surely there is a way of doing this in python.

Richard

#!/usr/bin/env python

import Cookie, os
import cgitb;cgitb.enable()

def getCookie():
	c = Cookie.SimpleCookie()
	if 'HTTP_COOKIE' in os.environ:
		c.load(os.environ['HTTP_COOKIE'])
		print "Found a Cookie", c, "<BR>"
		c['mysession'].value += 1
		print "<HR>"
		return c
	else:
		c['mysession'] = 45
		print "No Cookie Found so setting an initial value for a Cookie", c
		print "<HR>"
		return c

if __name__ == '__main__':

	print "Content-type: text/html\n\n"
	myCookie = getCookie()
	#Print all Environment variables
	for k, v in os.environ.items():
		print k, "=", v, "<BR>"
	print "<HR>"

print """
<form  method="get">
<input type="text" name="user" value="">
</form>
"""




More information about the Python-list mailing list