By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Cookie: Not understanding again

P: n/a
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>
"""

Jun 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 1 Jun, 15:49, mosscliffe <mcl.off...@googlemail.comwrote:
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>
"""
Forgot to add running python 2.3.4 on a hosted Apache server

Jun 1 '07 #2

P: n/a
On Jun 1, 3:49 pm, mosscliffe <mcl.off...@googlemail.comwrote:
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
Jun 2 '07 #3

P: n/a
On 2 Jun, 01:13, David Wahler <dwah...@gmail.comwrote:
On Jun 1, 3:49 pm, mosscliffe <mcl.off...@googlemail.comwrote: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>
David,

Thanks for your help.

I spent all last night trying to get it to work, but I can not work
out how to force my cookie into the response header. The most
annoying part is on one occasion it did and the cookie sat there until
I killed the session. By the time I discovered 'pageinfo' add-in for
firefox, the cookie had gone.

I have read a lot about cookies, but the grey matter is very slow at
absorbing nowadays. I was wondering if I should add the 'path=/' to
my cookie, as I am using a host that has numbers for the IP address
and my domain name when used, is via a frame. Although the python bit
to display the contents (when it did work), did not show any value for
'path'.

Could I create the response header in python and get it executed as
part of my form submission ?

Obviously, still struggling.

Richard
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

Jun 2 '07 #4

P: n/a
On Jun 2, 9:32 am, mosscliffe <mcl.off...@googlemail.comwrote:
David,

Thanks for your help.

I spent all last night trying to get it to work, but I can not work
out how to force my cookie into the response header. The most
annoying part is on one occasion it did and the cookie sat there until
I killed the session. By the time I discovered 'pageinfo' add-in for
firefox, the cookie had gone.
Your CGI script needs to produce output of the form:

Header-1: ...
Header-2: ...

response data goes here...

Each line before the blank line is an HTTP header. To send cookies to
the browser, you need to generate a header of the form:

Set-Cookie: name=value

Printing the SimpleCookie object does this for you.
I have read a lot about cookies, but the grey matter is very slow at
absorbing nowadays. I was wondering if I should add the 'path=/' to
my cookie, as I am using a host that has numbers for the IP address
and my domain name when used, is via a frame. Although the python bit
to display the contents (when it did work), did not show any value for
'path'.
If you specify a path attribute, then the browser will only send the
cookie on requests that begin with that path. If you don't, it
defaults to the URL of the page that generates the cookie. For a
single test script like this, there's no need to bother with it, but
you can set it like this:
>>cookie["someCookieName"]["path"] = "/path/to/whatever"
Could I create the response header in python and get it executed as
part of my form submission ?

Obviously, still struggling.
Here's a simple, complete example to get you started, based loosely on
your original code:

################################################## ####################

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

def getCookie():
c = Cookie.SimpleCookie()
if 'HTTP_COOKIE' in os.environ:
c.load(os.environ['HTTP_COOKIE'])

if os.environ['QUERY_STRING'] == 'reset' or 'mysession' not in c:
c['mysession'] = 0
else:
c['mysession'] = int(c['mysession'].value) + 1

return c

if __name__ == '__main__':

print "Content-type: text/html"
myCookie = getCookie()
print myCookie

if os.environ['QUERY_STRING'] == 'reset':
print "Status: 302 Moved"
print "Location:", os.environ['SCRIPT_NAME']
print
else:
print
print 'Current Value: ', myCookie['mysession'].value
print '<br><a href="?reset">Reset Counter</a>'

################################################## ####################

Hope this helps!

-- David

Jun 2 '07 #5

P: n/a
On 2 Jun, 11:50, David Wahler <dwah...@gmail.comwrote:
On Jun 2, 9:32 am, mosscliffe <mcl.off...@googlemail.comwrote:
David,
Thanks for your help.
I spent all last night trying to get it to work, but I can not work
out how to force mycookieinto the response header. The most
annoying part is on one occasion it did and thecookiesat there until
I killed the session. By the time I discovered 'pageinfo' add-in for
firefox, thecookiehad gone.

Your CGI script needs to produce output of the form:

Header-1: ...
Header-2: ...

response data goes here...

Each line before the blank line is an HTTP header. To send cookies to
the browser, you need to generate a header of the form:

Set-Cookie: name=value

Printing the SimpleCookie object does this for you.
I have read a lot about cookies, but the grey matter is very slow at
absorbing nowadays. I was wondering if I should add the 'path=/' to
mycookie, as I am using a host that has numbers for the IP address
and my domain name when used, is via a frame. Although the python bit
to display the contents (when it did work), did not show any value for
'path'.

If you specify a path attribute, then the browser will only send thecookieon requests that begin with that path. If you don't, it
defaults to the URL of the page that generates thecookie. For a
single test script like this, there's no need to bother with it, but
you can set it like this:
>>cookie["someCookieName"]["path"] = "/path/to/whatever"
Could I create the response header in python and get it executed as
part of my form submission ?
Obviously, still struggling.

Here's a simple, complete example to get you started, based loosely on
your original code:

################################################## ####################

importCookie, os
import cgitb; cgitb.enable()

def getCookie():
c =Cookie.SimpleCookie()
if 'HTTP_COOKIE' in os.environ:
c.load(os.environ['HTTP_COOKIE'])

if os.environ['QUERY_STRING'] == 'reset' or 'mysession' not in c:
c['mysession'] = 0
else:
c['mysession'] = int(c['mysession'].value) + 1

return c

if __name__ == '__main__':

print "Content-type: text/html"
myCookie = getCookie()
print myCookie

if os.environ['QUERY_STRING'] == 'reset':
print "Status: 302 Moved"
print "Location:", os.environ['SCRIPT_NAME']
print
else:
print
print 'Current Value: ', myCookie['mysession'].value
print '<br><a href="?reset">Reset Counter</a>'

################################################## ####################

Hope this helps!

-- David
David,

Thank you very much for your detailed reply. As soon as I get a
chance to try it all out, I will let you know how I get on.

Thanks again

Richard

Jun 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.