Cookies

Victor Subervi victorsubervi at gmail.com
Wed Dec 30 11:24:20 EST 2009


On Wed, Dec 30, 2009 at 11:54 AM, Carsten Haese <carsten.haese at gmail.com>wrote:

> Victor Subervi wrote:
> > Here again is my code:
> >
> > #! /usr/bin/python
> >
> > import string
> > import cgitb; cgitb.enable()
> > import MySQLdb
> > import cgi
> > import sys,os
> > sys.path.append(os.getcwd())
> > from login import login
> > import datetime, Cookie, random
> > from particulars import title
> > from templateFrame import top, bottom
> > from particulars import myCookie
> > import time
> > import fpformat
> > from sets import Set
> > from particulars import ourOptions
> >
> > def cart():
> >   print '''Content-Type: text/html\r\n
> > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> > <html>
> > '''
> >   cookie = os.environ.has_key('HTTP_COOKIE')
> >   if not cookie:
> >     cookie = Cookie.SimpleCookie()
> >     cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
> >     cookie['lastvisit'] = str(time.time())
> >     cookie['lastvisit']['expires'] = cExpires
> >     cookie['lastvisit']['path'] = cPath
> >     cookie['lastvisit']['comment'] = cComment
> >     cookie['lastvisit']['domain'] = cDomain
> >     cookie['lastvisit']['max-age'] = cMaxAge
> >     cookie['lastvisit']['version'] = cVersion
> >     cookie['mycookie'] = 'mycookie'
> >     cookieFlag = 'new'
> >   else:
> >     cookie = Cookie.SimpleCookie(cookie)
> >     cookieFlag = 'old'
> > #  Don't know what to do with this. It's for when client won't accept
> > cookies
> > #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
> > #  session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
> > #  session['lastvisit'] = repr(time.time())
> > #  session.close()
> >   print cookieFlag
> >   cookie.load(cookie)
> >   print cookie
> >  ...
> >
> > Please advise.
>
> Pardon me for not being able to read your mind. The code you're now
> posting is significantly more code than your first post included. The
> line that I had determined to be missing is in fact not missing, you
> just didn't bother to post your actual code.
>

As my British father would have said, you're a gentleman and a scholar...


>
> Now that you've posted your actual code, I can see that the actual
> problem is that the line is in the wrong place.
>
> The line in question is <<print cookie>>, and it's responsible for
> printing a "Set-Cookie" *HEADER*, which is a fact that at least one of
> the tutorials you pointed out has mentioned. You're printing it where it
> doesn't belong, in the middle of your HTML output. You need to
> reorganize your print statements such that the Set-Cookie header is
> printed in the header of your script's output.
>

I've revised the code thus:

  cookie = os.environ.has_key('HTTP_COOKIE')
  if not cookie:
    cookie = Cookie.SimpleCookie()
    cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
    cookie['lastvisit'] = str(time.time())
    cookie['lastvisit']['expires'] = cExpires
    cookie['lastvisit']['path'] = cPath
    cookie['lastvisit']['comment'] = cComment
    cookie['lastvisit']['domain'] = cDomain
    cookie['lastvisit']['max-age'] = cMaxAge
    cookie['lastvisit']['version'] = cVersion
    print cookie
    cookieFlag = 'new'

Unfortunately, this still prints "new" for the cookieFlag no matter how many
times I refresh. Please advise.
TIA,
beno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091230/24314e57/attachment-0001.html>


More information about the Python-list mailing list