Cookie: Not understanding again

M

mosscliffe

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>
"""
 
M

mosscliffe

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
 
D

David Wahler

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
 
M

mosscliffe

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

David Wahler

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
 
M

mosscliffe

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top