Web Example Using Tornado-Asynchronous networking

Below are the Web example of Tornado-Asynchronous networking:

File Structure

main.py : Below, code defines a Tornado web application with two routes: “/” handled by MainHandler and “/about” handled by AboutHandler. MainHandler renders “index.html” with a message, while AboutHandler renders “about.html”. When executed, the Tornado server listens on port 8888 and prints a message indicating its running status.

Python3
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html", message="Hello, Tornado!",
                    host=self.request.host)

    def post(self):
        self.redirect("/about")


class AboutHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("about.html")

    def post(self):
        self.redirect("/")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r"/about", AboutHandler),

    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    print("Tornado server running at http://localhost:8888")
    tornado.ioloop.IOLoop.current().start()

Creating GUI

index.html : Below HTML code defines a webpage with the title “Hello Tornado!”. It includes a header displaying a message variable and a paragraph displaying the host variable. Additionally, there’s a form with a button that redirects to the “/about” route when submitted. The stylesheet “styles.css” is linked for styling purposes.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Tornado!</title>
    <link rel="stylesheet" type="text/css" href="/static/styles.css">
</head>
<body>
    <div class="container">
        <h1>{{ message }}</h1>
        <p>Host Address: {{ host }}</p>
        <form action="/" method="post">
            <button type="submit">Go to About</button>
        </form>
    </div>
</body>
</html>

about.html: Below, HTML code defines a webpage titled “About Us”. It contains a header displaying “About Us”, a paragraph describing the page content, and a form with a button that redirects to the “/about” route when submitted. The stylesheet “styles.css” is linked for styling purposes.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>About Us</title>
    <link rel="stylesheet" type="text/css" href="/static/styles.css">
</head>
<body>
    <div class="container">
        <h1>About Us</h1>
        <p>This is a simple Tornado web application.</p>
        <form action="/about" method="post">
            <button type="submit">Go to Home</button>
        </form>
    </div>
</body>
</html>

Run the Server

python main.py

Output:

Python Tornado – Asynchronous Networking

Traditional synchronous networking models often struggle to keep pace with the demands of modern applications. Enter Tornado-Asynchronous networking, a paradigm-shifting approach that leverages non-blocking I/O to create lightning-fast, highly scalable network applications. In this article, we will learn about Python Tornado – asynchronous networking.

What is Python Tornado – Asynchronous Networking?

Tornado is a Python web framework and asynchronous networking library that has gained significant popularity due to its ability to handle thousands of simultaneous connections with ease. At its core, Tornado utilizes an event-driven architecture, allowing it to handle I/O operations asynchronously without the need for multithreading.

Similar Reads

Python Tornado – Asynchronous Networking

Below are the Examples of Tornado-Asynchronous Networking in Python:...

Web Example Using Tornado-Asynchronous networking

Below are the Web example of Tornado-Asynchronous networking:...

Advantages of Tornado Asynchronous Networking

The advantages of Tornado-Asynchronous networking are numerous:...

Contact Us