Introduction to Tornado (1): Python web framework started with Tornado

Introduction

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.

Target audience

Required environment

About Tornado

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.

Let's use Tornado!

Installation

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. スクリーンショット 2015-10-13 0.11.50.png

Code description

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.

Application object

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/".

Reference material

Recommended Posts

Introduction to Tornado (1): Python web framework started with Tornado
Link to get started with python
Getting Started with Python Web Applications
How to get started with Python
Getting Started with Python Web Scraping Practice
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Python] Let's use foreach with Python
[Python] Introduction to CNN with Pytorch MNIST
Getting Started with Python Web Scraping Practice
Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
[FastAPI] Getting started with FastAPI, an ASGI web framework made by Python
Django 1.11 started with Python3.6
[Python] Easy introduction to machine learning with python (SVM)
Introduction to Artificial Intelligence with Python 1 "Genetic Algorithm-Theory-"
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
Markov Chain Chatbot with Python + Janome (2) Introduction to Markov Chain
Django python web framework
Introduction to Artificial Intelligence with Python 2 "Genetic Algorithm-Practice-"
1.1 Getting Started with Python
Introduction to Web Scraping
Getting Started with Python
Introduction to Python language
Try using the Python web framework Tornado Part 1
Introduction to OpenCV (python)-(2)
Introduction to formation flight with Tello edu (Python)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
Introduction to Python with Atom (on the way)
Introduction to Generalized Linear Models (GLM) with Python
Getting Started with Python
Try using the Python web framework Tornado Part 2
[Introduction to Udemy Python3 + Application] 9. First, print with print
A layman wants to get started with Python
Materials to read when getting started with Python
[Introduction to Python] How to iterate with the range function?
I tried to get started with blender python script_Part 01
[Chapter 5] Introduction to Python with 100 knocks of language processing
An introduction to Python distributed parallel processing with Ray
Introduction to Mathematics Starting with Python Study Memo Vol.1
Reading Note: An Introduction to Data Analysis with Python
I tried to get started with blender python script_Part 02
Super Primer to python-Getting started with python3.5 in 3 minutes
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Connect to BigQuery with Python
Getting Started with Python Functions
Web scraping with python + JupyterLab
Introduction to Python Django (2) Win
Connect to Wikipedia with Python
Post to slack with Python 3
Getting Started with Python Django (1)
Introduction to RDB with sqlalchemy Ⅰ
Getting Started with Python Django (4)
Introduction to serial communication [Python]
Getting Started with Python Django (3)
Web API with Python + Falcon
Switch python to 2.7 with alternatives
Write to csv with Python
[Introduction to Python] <list> [edit: 2020/02/22]