micro-framework in Python 2.5 & 2.6. • Python 3 port underway • Developed at FriendFeed and open-sourced by Facebook • Similar to web.py in use • Fast: ~1,500 requests/sec backend* * Your milage will vary
• Third Party Authentication via OpenID, OAuth Mixins • Light-weight template system • Auto-magical cross-site forgery protection • WSGI && Google App Engine Support • Develop using debug mode and automatically reload code and templates when changed on disk
handlers that make up a web application. Instances of this class are callable and can be passed directly to HTTPServer to serve the application: application = web.Application([ (r"/", MainPageHandler), ]) http_server = httpserver.HTTPServer(application) http_server.listen(8080) ioloop.IOLoop.instance().start() The constructor for this class takes in a list of URLSpec objects or (regexp, request_class) tuples. When we receive requests, we iterate over the list in order and instantiate an instance of the first request class whose regexp matches the request path. Each tuple can contain an optional third element, which should be a dictionary if it is present. That dictionary is passed as keyword arguments to the contructor of the handler. This pattern is used for the StaticFileHandler below: application = web.Application([ (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), ])
• Based on Twisted • There is an unmaintained port, Tornado on Twisted • Influenced the Cyclone project • A replacement for a front-end web server • Run behind a reverse proxy http server (nginx, Cherokee)
• It doesn’t have as many asynchronous drivers • Can introduce blocking behaviors • The Tornado code is smaller and very easy to understand • Less mature than Twisted • You don’t need to buy into a development methodology • Write Python not Twisted
Multiple classes used in a web application • Includes decorators • Asynchronous function: @tornado.web.asynchronous • Authentication Required: @tornado.web.authenticated
get, head, post, delete, put, options • Hooks on Initialization, Prepare, Close • Functions for setting HTTP Status, Headers, Cookies, Redirects and more
self.request.headers or \ self.request.headers['X-Forwarded-Ssl'] != 'on': return self.render_string("modules/ssl.html") return '' <div class="information"> <a href="https://{{request.host}}{{request.uri}}"> {{_("Click here to use a secure connection")}} </a> </div> <div>{{ modules.HTTPSCheck() }}</div> UIModule Class Template Embed
csv format • Named as locale.csv, e.g. en_US.csv • tornado.locale.load_translations(pa th) • Pass path where files are located • Invoked as _ method in templates
def get_user_locale(self): # Fake user object has a get_locale() function user_locale = self.user.get_locale() # If our locale is supported return it if user_locale in locale.get_supported_locales(None): return user_locale # Defaults to Accept-Language header if supported return None
Google, Twitter, Facebook, Facebook Graph, Friendfeed • Use RequestHandler to extend your own login functions with the mixins if wanted • Is asynchronous • Not supported in WSGI and Google App Engine