Cookies are used by web developers or designers to store data on the client side. When user submits some information to the server, the server takes a decision based on posted data and redirects the user to relevant page. But when user browses to all together a different page, this information is lost. Cookies help to store this information across web pages. Cookies could be persistent (until expired) or temporary (deleted when browser is closed) as designed by developer.
Tornado has methods can set/get cookies. Let see how with this example:
In the above example,
1. When user browses to http://127.0.0.1:8888/user/, web server checks if it finds any cookie with name ‘technobeans’. If not, it sets a cookie with name ‘technobeans’ and renders a web page stating ‘Cookie is now set’.
2. If this web page is refreshed (with cookie still set), web page renders a message ‘Cookie is technobeans’.
User behavior in the above two cases is mimicked with tornadocookierequest.py and tornadocookieresponse.txt depicts the response and content returned by web server for the requests.
Deleting a cookie
Cookies can be cleared with a method clear_cookie(). For instance, if cookie with name “user” needs to be deleted, it can be done as