Cookies

Tim O'Malley timo at bbn.com
Thu Jan 13 11:57:19 EST 2000


hola.

Anders M Eriksson writes:
 > kaka = Cookie.Cookie()
 > try:
 > 	nPos = kaka['nPos'].value
 > except:
 > 	nPos = 0
 > 
 > for i in range(20)
 > 	npos = npos + 1
 > print "Content-type: text/html\n"
 > print `nPos`
 > 
 > kaka['nPos'] = nPos

You've got lots of problems here:
  - you switch the case of nPos in the for loop
  - you never print out the cookie, so it never gets sent to the client

Try something like this (untested):

import os
import Cookie

# Create a cookie
#    If the env. var. HTTP_COOKIE does not exist, then
#    we create an empty cookie.
kaka = Cookie.SmartCookie( os.environ.get("HTTP_COOKIE", "") )

# Set nPos to the cookie's value, if it exists
# If the cookie doesn't include nPos, then default to a value of 0.
try:
    nPos = kaka["nPos"].value
except:
    nPos = 0
    
# I do not know why anyone would do this, but..
for i in range(20):
    nPos = nPos + 1

# Set the cookie's new value
kaka["nPos"] = nPos

# Send a response to the client
#   Include the cookie in the headers
#   Put the value of nPos on the page

print "Content-type: text/html"
print kaka
print

print "<HTML><BODY>%d</BODY></HTML>" % nPos

-- 
.....................................................................
.   happy daze            .   Age and treachery will beat           .
.        -tim O           .       youth and skill every time.       .
.....................................................................




More information about the Python-list mailing list