This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

What is the Tornado

The school committee found tornado web framework when looking at the source code of Jupyter components.

Rather than just being a Web framework, Tornado can scale to tens of thousands of open connections by using non-blocking network I/O.

This is ideal for long Polling, WebSockets, and other applications that require a long-term connection to each user.

Ok, let’s try it out.

The installation

pip install tornado
Copy the code

If you don’t know how to use PIP, check out # sec-master PIP and quickly experience the deep learning app! 【 Multi-picture Suggestions 】

Try writing a Web application using the Tornado framework

Let’s do a simple, save the following code as the app.py file

"" lei School Committee # Code Demo """

import tornado.ioloop
importWrite a request handlerclass MainHandler(tornado.web.RequestHandler) :
    def get(self) :
        self.write("Hello, Tornado Server");


""" create a Web App """
def make_app() :
    # Bind interface "/" to MainHandler
    return tornado.web.Application([
	    (r"/", MainHandler),	
    ])


# Program entry
if __name__ == '__main__':
    app = make_app()
    app.listen(8080)
    tornado.ioloop.IOLoop.current().start()
Copy the code

Run as follows :(python app.py)

The effect is as follows:

This example is simple, so learn to use it.

Try making a form

"" lei School Committee # Code Demo """

import tornado.ioloop
import tornado.web

A request handler that processes forms
class MyFormHandler(tornado.web.RequestHandler) :
    def get(self) :
        # Direct page render a form
        self.write('<html><body class="leiXueWei"><form action="/form" method="POST">'
                   '<input type="text" name="message">'
                   '<input class="leiXueWei" type="submit" value="Submit">'
                   '</form></body></html>')

    def post(self) :
        self.set_header("Content-Type"."text/plain")
        self.write("You wrote " + self.get_body_argument("message"))


class MainHandler(tornado.web.RequestHandler) :
    def get(self) :
        self.write("Hello, Tornado Server");

def make_xue_wei_app() :
    Use application to load multiple routes: "/" interface route and "/form" interface route
    return tornado.web.Application([
	    (r"/", MainHandler),
        (r"/form", MyFormHandler)
    ])


if __name__ == '__main__':
    app = make_xue_wei_app()
    app.listen(18081)
    tornado.ioloop.IOLoop.current().start()
Copy the code

This code has one more request handler and route load for the form than in the first example.

The submit button initiates the POST request directly to the “/form” interface without any js embedded.

And the blogger also didn’t write any JS processing pages, which is very simplified. This “little magic” is one reason why this article wants to recommend Tornado!

  

The above essay, simple use of sharing, and then continue to push the interpretation of the framework.

By the way, the student committee also has this can pay attention to long-term reading => lei Student committee interesting programming story compilation or => Lei Student Committee NodeJS series

Continuous learning and continuous development, I am the Lei School committee! Programming is fun, but the key is to get the technology straight. Creation is not easy, please support a lot, click like collection to support the school committee!

Reference links:

For use of Pip => juejin.cn/post/698259…

Tornado => www.osgeo.cn/tornado/