Cookies

Victor Subervi victorsubervi at gmail.com
Wed Dec 30 04:27:31 EST 2009


On Tue, Dec 29, 2009 at 3:43 PM, Carsten Haese <carsten.haese at gmail.com>wrote:

> Victor Subervi wrote:
> > Hi;
> > I have these lines:
> >
> >   cookie = os.environ.get('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
> >     cookieFlag = 'new'
> >   else:
> >     cookie = Cookie.SimpleCookie(cookie)
> >     cookieFlag = 'old'
> >   print '''Content-Type: text/html\r\n
> > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> > <html>
> > '''
> >   print cookieFlag
> >
> > cookieFlag prints 'new'. Every time. Even when I refresh. I've imported
> > Cookie. I've followed the tutorials :-} I've already been through my
> > problem with trying to find the cookies on this computer. The fact that
> > I'm using gmail is proof that cookies are enabled. I have also tried:
> > cookie = os.environ.has_key('HTTP_COOKIE')
> > Please advise.
>
> You apparently haven't followed the tutorials carefully enough. You do
> know that a cookie is a piece of information that's stored in your
> browser, don't you? So tell me, which of the above lines of code do you
> suppose is responsible for informing your browser of the cookie's contents?
>

This one:

cookie = SimpleCookie()

In the tutorial found here, for example:

http://www.doughellmann.com/PyMOTW/Cookie/index.html

I read the following:

<snip>
Cookies are used as state management, and as such as usually set by the
server to be stored and returned by the client. The most trivial example of
creating a cookie looks something like:

import Cookie

c = Cookie.SimpleCookie()
c['mycookie'] = 'cookie_value'
print c

The output is a valid Set-Cookie header ready to be passed to the client as
part of the HTTP response:

$ python Cookie_setheaders.py
Set-Cookie: mycookie=cookie_value
</snip>

Do not the printout words "Set-cookie" indicate the cookie has been set?
Furthermore, none of the tutorials I have found state anything differently.
Another example is found here:

http://webpython.codepoint.net/cgi_set_the_cookie

Please advise.
TIA,
beno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091230/30379450/attachment.html>


More information about the Python-list mailing list