Cookie: Not understanding again

David Wahler dwahler at gmail.com
Fri Jun 1 20:13:04 EDT 2007


On Jun 1, 3:49 pm, mosscliffe <mcl.off... at googlemail.com> wrote:
> 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.
>
<snip code>

The cookie values have to be sent to the browser as part of the
response headers. (Modifying the Cookie object doesn't do this for you
-- the Cookie instance just acts as a container.) In other words,
something like this:

c = Cookie.SimpleCookie()
c.load(os.environ['HTTP_COOKIE'])
# ...modify cookie object...
print "Content-Type: text/html"
print c
print
# send the HTML text

-- David





More information about the Python-list mailing list