Cookies

Carsten Haese carsten.haese at gmail.com
Wed Dec 30 08:56:35 EST 2009


Victor Subervi wrote:
> On Tue, Dec 29, 2009 at 3:43 PM, Carsten Haese <carsten.haese at gmail.com
> <mailto:carsten.haese at gmail.com>> wrote:
>     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?

That's exactly what that printout indicates, and ONLY that printout
indicates that. Without that printout, no cookie will be set in the
browser, because that printout is what transports the contents of the
cookie to the browser. However, the line <<c = Cookie.SimpleCookie()>>
is not responsible for producing that printout, which can be tested
easily in a Python command line:

py> import Cookie
py> c = Cookie.SimpleCookie()
py>

As you can see, no printout is generated.

By the way, expecting a line of code to produce content that is
established in lines of codes that follow it requires a mental execution
model of Python that involves magic, mind reading, or time travel, none
of which are features Python is known for.

So, guess again. The "trivial example" has three more lines of code. One
of them is unlike any line you have in your code. That's the line
responsible for producing the "Set-Cookie" header, and that's the line
you're missing.

--
Carsten Haese
http://informixdb.sourceforge.net




More information about the Python-list mailing list