Karrigell/Set, read and erase cookies

Cookies are managed by two variables available in the scripts :
 * is a dictionary-like object holding the cookies sent by the browser and received by the script
 * is a dictionary-like object used by the script to set cookies values, path, expiry date etc. and send them to the browser

Here is a simple script, cookies.py, that shows how to set, read and erase cookies :

In, the cookies sent by the browser are printed. Then a form allows the user to set another cookie, with a specified name and value

This form is submitted to the function. In this function, a new entry is created in  with the submitted data. The key 'path' of this entry is set to the server root. Then a redirection is performed to the  function

When this redirection is done, the newly created cookie is sent to the browser. The browser stores the value and its attribute ; then it sends a request to the url

When this request is sent, the request headers include this cookie, received by the script in the variable  : so the cookie name and value are printed at the top of the page

To erase the cookie, a link is provided, leading to the function  where name is the cookie name. The function  uses   to erase the value, and set the expiry date to the past (10 days before today), using the function   to transform a datetime object to the format supported by the cookie specifications

At last, when these values are set for the cookie, another redirection is performed to  : the browser receives the new information about the specified cookie. Since the expiry date is reached, it erases it. When it sends a request to, it does not send a header for this cookie, which does not appear in the received cookies list