In this series, I will introduce how to develop applications with Tornado, which is a web server & web application framework that runs on Python. The contents of this series are as follows.
In this introduction, we will first introduce the features of Tornado, the environment setting procedure, and the creation of the "Hello, World" application.
Tornado is an open source, non-blocking web server and web framework based on Apache License 2.0. Tornado is a framework developed by FriendFeed that was absorbed by Facebook and then open sourced, with the following features:
Whereas Django is a full-stack framework, Tornado is a type of framework that combines the components you need. Tornado is extremely versatile and has a wealth of original documentation. In contrast, there is little information in Japanese, but I think it will be very useful if it can be utilized.
Install with pip as follows.
$ pip install tornado
Hello World Below is the official Hello World code. It's a short code with minimal writing, but it works.
app.py
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.current().start()
Save the above code as app.py and try running it like this:
$ python app.py
If you access the local host number 8888 ([http: // localhost: 8888 /](http: // localhost: 8888 /)) and the following Hello World characters are displayed, it is successful.
I will explain the following two important points in app.py.
RequestHandler The RequestHandler class is the base class for processing HTTP requests. In order to handle the actual request, you need to create a subclass of the RequestHandler class. Take a look at the code snippet below.
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
In this piece of code, the RequestHandler class is inherited to create the MainHandler class. When handling a request, Tornado instantiates this MainHandler class and calls the method that corresponds to the HTTP method of the request. In this example, only the get method is defined, so it can only handle HTTP GET requests. In addition to the get method, there are post, delete, put methods, etc. that support POST, DELETE, PUT, etc. In this way, the method to be processed can be automatically separated according to the type of HTTP request, so one method can be simplified.
The Application object is used to configure application-wide settings.
application = tornado.web.Application([
(r"/", MainHandler),
])
This piece of code creates an instance of the Application class. The important thing is to pass a list of tuples as an argument when creating the Application instance. This tuple describes the correspondence between the URL and the handler. This tells Tornado which handler should handle which request. In the case of the above example, it means that when "http://example.com/" is accessed, the processing is performed by MainHandler. If you wrote r "/ hoge /", you need to access "http://example.com/hoge/".
Recommended Posts