Using Tornado routing mechanism
In this tutorial, we will explore the routing mechanism of the Tornado web framework. Routing is an essential concept in web development. It is the process of defining how an application responds to a client request to a particular endpoint or URI. Let's dive into the details.
What is Tornado?
Tornado is a Python web framework and asynchronous networking library. It makes use of non-blocking network I/O and is designed to handle thousands of simultaneous connections, making it ideal for real-time web services.
Basic Routing in Tornado
The fundamental building block of a Tornado web application is the Application
class. It is responsible for setting up the server and defining the routing table. Here is a simple example:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
In this example, we define a simple GET
request handler called MainHandler
. We then create an instance of tornado.web.Application
and pass in a list of tuples. Each tuple contains a regular expression (regex) to match the request URL and the corresponding RequestHandler class.
Parameterized Routing
Tornado routing also supports parameterized URLs. For instance, you can capture a portion of the URL as a variable and pass it to your handler:
import tornado.ioloop
import tornado.web
class ProfileHandler(tornado.web.RequestHandler):
def get(self, username):
self.write(f"Hello, {username}")
def make_app():
return tornado.web.Application([
(r"/profile/(.*)", ProfileHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
In this example, any URL that matches the pattern /profile/username
will be handled by ProfileHandler
, and the username
will be passed as an argument to the get
method.
Named Parameters in Routing
Tornado supports named parameters in the URL, which can make your code more readable and maintainable. Here's an example:
import tornado.ioloop
import tornado.web
class ProfileHandler(tornado.web.RequestHandler):
def get(self, username):
self.write(f"Hello, {username}")
def make_app():
return tornado.web.Application([
(r"/profile/(?P<username>.*)", ProfileHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
In this case, the URL pattern includes a named group (?P<username>.*)
. The username
in the URL will be passed as a keyword argument to the get
method.
Routing to Multiple Handlers
A Tornado application can route different URLs to different handlers. Let's see an example:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
class ProfileHandler(tornado.web.RequestHandler):
def get(self, username):
self.write(f"Hello, {username}")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/profile/(.*)", ProfileHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
In this example, the application routes the root URL (/
) to MainHandler
and any URL of the form /profile/username
to ProfileHandler
.
Remember, Tornado's routing is quite powerful and flexible. It allows us to build web applications that can respond to various URI patterns with appropriate actions. This tutorial has introduced you to the basics of Tornado routing. As you continue to explore Tornado, you'll find that it provides a lot more features and options. Happy Tornado learning!