Tornado – Cookies

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:

import tornado.ioloop
import tornado.web
import time
class Hello(tornado.web.RequestHandler):
def get(self):
self.write("Hello there")
class User(tornado.web.RequestHandler):
def get(self):
cookieName = "technobeans"
if not self.get_cookie(cookieName):
self.set_cookie(cookieName, str(time.time()))
self.write("Cookie is now set")
else:
self.write("Cookie is " + cookieName)
application = tornado.web.Application([
(r"/", Hello),
(r"/user/", User),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

view raw
tornadocookie.py
hosted with ❤ by GitHub

import httplib2
h = httplib2.Http()
response, content = h.request("http://127.0.0.1:8888/user/", "GET")
print"HTTP GET request"
print "Reponse:", response, "\nContent:", content, "\nCookie:", response['set-cookie']
## Resending the request with cookie with headers
headers = {"Cookie":response['set-cookie']}
response_2, content_2 = h.request("http://127.0.0.1:8888/user/", "GET", headers = headers)
print "\nResending the request with cookie in headers"
print "Reponse:", response_2, "\nContent:", content_2

HTTP GET request
Reponse: {'status': '200', 'content-length': '17', 'content-location': 'http://127.0.0.1:8888/user/', 'set-cookie': 'technobeans=1344314694.4; Path=/', 'server': 'TornadoServer/2.2', 'etag': '"25dcbbc84ce1fedc0d4bc15abdc23c4abb1a4006"', 'content-type': 'text/html; charset=UTF-8'}
Content: Cookie is now set
Cookie: technobeans=1344314694.4; Path=/
Resending the request with cookie in headers
Reponse: {'status': '200', 'content-length': '21', 'content-location': 'http://127.0.0.1:8888/user/', 'server': 'TornadoServer/2.2', 'etag': '"95bb30558d14b3d9eb37a1f1fccc5c84f546ce3a"', 'content-type': 'text/html; charset=UTF-8'}
Content: Cookie is technobeans

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

self.clear_cookie("user")

One thought on “Tornado – Cookies

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.