Professional Documents
Culture Documents
Gavin M. Roy
CTO
myYearbook.com
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
tornado.web.Application
Initialization Options:
Route to request handlers
Default host
Settings
Transforms
WSGI
tornado.web.Application Settings
import redis
import tornado.web
class MyRequestHandler(tornado.web.RequestHandler):
def initialize(self):
host = self.application.settings['Redis']['host']
port = self.application.settings['Redis']['port']
class Homepage(MyRequestHandler):
@tornado.web.asynchronous
def get(self):
content = self.redis.get('homepage')
self.write(content)
self.finish()
tornado.ioloop
Protocol independent
tornado.httpserver.HTTPServer implemented
using the ioloop
Implemented a Tornado adapter for Pika
using it
Built in timer functionality
tornado.ioloop.add_timeout
tornado.ioloop.PeriodicCallback
tornado.ioloop Example
class MyServer(object):
self.sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM, 0)
self.sock.connect((host, port))
self.sock.setblocking(0)
self.io_loop = tornado.ioloop.IOLoop.instance()
self.handle_connection_open()
# Append our handler to tornado's ioloop for our socket
events = tornado.ioloop.IOLoop.READ |
tornado.ioloop.IOLoop.ERROR
self.io_loop.add_handler(self.sock.fileno(),
self._handle_events, events)
https://github.com/gmr/pika/blob/master/pika/tornado_adapter.py
tornado.template
Not required
Similar to other engines
Limited python exposure in template
Fast, extensible
Built in to RequestHandler functionality
RequestHandler.render
class Home(RequestHandler):
def get(self):
<html>
<body>
Hi {{username}}, welcome to our site.
</body>
</html>
Template Example
<html>
<head>
<title>eMuse :: {% block title %}Unextended Template{% end %}</title>
<link rel="stylesheet" type="text/css" href="{{static_url('css/site.css')}}" />{% block css %}{%
end %}
<script type="text/javascript" src="{{static_url('javascript/site.js')}}"></script>
{% block javascript %}{% end %}
{% if not current_user %}<script type="text/javascript" src="http://api.recaptcha.net/js/
recaptcha_ajax.js"></script>{% end %}
</head>
<body{% if current_user %} class="authenticated"{% end %}>
{% include "header.html" %}
{% if request.uri not in ['/', ''] and current_user %}
{{modules.MemberBar()}}
{% end %}
<div id="content">
{% block content %}
No Content Specified
{% end %}
</div>
<ul id="footer">
<li><a href="/terms">{{_("Terms and Conditions")}}</a></li>
<li class="center">{{_("Version")}}: {{handler.application.settings['version']}}</li>
<li class="right">{{_("Copyright")}} © {{ datetime.date.today().year }} Poison Pen,
LLC</li>
</ul>
</body>
</html>
Template XSRF Example
class HTTPSCheck(tornado.web.UIModule):
def render(self):
<li class="information">
<a href="https://{{request.host}}{{request.uri}}">
{{_("Click here to use a secure connection")}}
</a>
</li>
{{ modules.HTTPSCheck() }}
tornado.locale
class RequestHandler(tornado.web.RequestHandler):
def get_user_locale(self):
<html>
<body>
{{_("Welcome to our site.")}}
</body>
</html>
tornado.auth
- [/login/form, emuse.auth_reg.LoginForm]
- [/login/friendfeed, emuse.auth_reg.LoginFriendFeed]
@tornado.web.asynchronous
def get(self):
if self.get_argument("oauth_token", None):
self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authorize_redirect()
username = ffuser['username']
Other Modules of Note
tornado.database tornado.options
tornado.escape tornado.testing
tornado.httpclient tornado.wsgi
tornado.iostream tornado.websocket