Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tornado - Secure Cookies
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
ubuntu@ubuntu:~/tornado-2.2$ python request.py
HTTP GET request
Reponse: {'status': '200', 'content-length': '17', 'content-location': 'http://127.0.0.1:8888/user/', 'set-cookie': 'technobeans="MTM0NDM1OTA5NS4yOQ==|1344359095|be3d68ebe6251359e9f8bade853ca99c93ff1daa"; expires=Thu, 06 Sep 2012 17:04:55 GMT; Path=/', 'server': 'TornadoServer/2.2', 'etag': '"25dcbbc84ce1fedc0d4bc15abdc23c4abb1a4006"', 'content-type': 'text/html; charset=UTF-8'}
Content: Secure Cookie is now set
Cookie: technobeans="MTM0NDM1OTA5NS4yOQ==|1344359095|be3d68ebe6251359e9f8bade853ca99c93ff1daa"; expires=Thu, 06 Sep 2012 17:04:55 GMT; 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: Secure Cookie is technobeans
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_secure_cookie(cookieName):
self.set_secure_cookie(cookieName, str(time.time()))
self.write("Secure Cookie is now set")
else:
self.write("Secure Cookie is " + cookieName)
application = tornado.web.Application([
(r"/", Hello),
(r"/user/", User),
], cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=")
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment