How to send cookies

Alex Martelli aleaxit at yahoo.com
Fri Jan 5 05:07:12 EST 2001


"David López" <dlopezp at norsistemas.es> wrote in message
news:newscache$cmho6g$plg$1 at news.cesatel.es...
> Thank you very much.
>
> Concretely our problem is that we don´t know how to SEND cookies. We
already
> have worked with Java cookies. We would thank you if you can ask our
> question.

Cookies are 'sent' (if enabled), as a part of some HTTP requests, by
the 'client', in 'Cookie: ' HTTP headers in the request.

Cookies are also 'sent' (if desired), as part of an HTTP response, by
the 'server', in 'Set-Cookie: ' HTTP headers in the response.

I suspect the latter 'sending' is what you're asking about?

Say, for example, that you are writing a CGI script in Python.  The
script will output (typically with 'print') the 'HTTP response' that
the requesting client will receive.

A typical toy response including no cookies might be, for example,
generated by this script whattime.py:

import time
print "Content-type: text/html"
print

print "<p>The time is now %s" % time.ctime(time.time())

When a client (e.g., a browser) visits this CGI script (e.g.,
with a GET of http://what.ever.com/cgi-bin/whattime.py) it
will receive an HTTP response whose headers end with
'Content-type: text/html' (the blank line given by the print
right after that terminates the headers), then an HTML body
of something like '<p>The time is now Fri Jan 05 10:49:29 2001'.

OK so far?

Good.  Now, if the CGI script also wants to 'send' (set) a
cookie, it will generate an extra header of 'Set-Cookie:'.

For example:

import time
print "Content-type: text/html"
print "Set-Cookie: greeting=hello"
print

print "<p>The time is now %s" % time.ctime(time.time())

That's all there is to "sending a cookie" (if by 'send' you
mean 'set'): just generate the appropriate Set-Cookie header,
and voila.

You can check that this is working by reading back the
cookie (that the client sends as the 'Cookie' header, so
you can access it as the HTTP_COOKIE environment variable,
if it exists):

import time, os
print "Content-type: text/html"
print "Set-Cookie: greeting=hello;"
print

print "<p>The time is now %s" % time.ctime(time.time())
if os.environ.has_key('HTTP_COOKIE'):
    print "<p>Cookie is %s" % os.environ['HTTP_COOKIE']
else:
    print "<p>No Cookie."

The first time a client visits this script, the response
will end with 'No Cookie'; on each following occasion, if
the client has cookies enabled, the response will end with
'Cookie is greeting=hello'.


Now, Python has a 'Cookie' module to help you prepare the
cookies you may want to send (set) -- but the 'sending'
(setting) itself is always done by having a suitable
'Set-Cookie' HTTP header in the response; the cookie
object helps by letting you use it as a dictionary and
generating the 'Set-Cookie' when you print it.

Here, for example, we set ('send') two cookies -- one,
as before, and one through the Cookie module:

import time, os, Cookie
print "Content-type: text/html"
print "Set-Cookie: greeting=hello;"
cookie = Cookie.SimpleCookie()
cookie['name']='Alex'
print cookie
print

print "<p>The time is now %s" % time.ctime(time.time())
if os.environ.has_key('HTTP_COOKIE'):
    print "<p>Cookie is %s" % os.environ['HTTP_COOKIE']
else:
    print "<p>No Cookie."

Now, after a first visit, on following visits the response
will end with 'Cookie is greeting=hello; name=Alex'.


I hope this answers your question "how to send cookies":
to summarize, the answer is "arrange for Set-Cookie HTTP
headers to be sent in your HTTP response, in whatever
way you like to so arrange (by preparing and printing
such headers directly, by preparing Cookie.SimpleCookie
objects and printing them as part of your response's
headers, whatever)".


Alex






More information about the Python-list mailing list