![]() |
Tornado is a robust, open-source web framework designed for building scalable and non-blocking web applications in Python. It was developed by FriendFeed (later acquired by Facebook) to handle long-lived network connections efficiently. Tornado is known for its high performance, simplicity, and asynchronous capabilities, making it an excellent choice for building real-time web applications. In this article, we will explore Tornado through three code examples, ranging from a simple “Hello World” application to more advanced features like form submission and file uploads. Tornado -Web FrameworkBelow, are the example of Tornado -Web Framework in Python. Example 1: Hello World in TornadoIn this example, below Python script below uses the Tornado web framework to create a basic web application. It defines a handler that responds with “Hello, Tornado!” to a GET request at the root (“/”) URL. The application listens on port 8888 and starts the Tornado I/O loop when executed. Python3
Run the Serverrun the tornado server using below command python script_name.py Ouput : Example 2: Form Submission in Tornadoapp.py : Now, let’s look at a more advanced example that involves handling form submissions. In this example, we create a form with an input field for the user’s name. The Tornado handler retrieves the submitted data using self.get_argument and responds with a personalized greeting. Python3
index.html : This HTML document defines a simple form with a “Name” input field and a submit button. It links to an external stylesheet (“style.css”) for styling, setting the form’s width and styling labels and inputs. The form is set to submit data to “/form” using the POST method. HTML
Run the Serverrun the tornado server using below command python script_name.py Output : Example 3: File Upload in Tornadoapp.py :This Tornado script sets up a file upload server with a single “/upload” route. The `UploadHandler` class handles GET requests by rendering an HTML upload form and processes POST requests by saving uploaded files to a ‘uploads’ directory. The server runs on port 8888. Python3
upload.html : This HTML document creates a simple file upload form. It includes a title, a file input field within a form, and a submit button. The form is set to submit data to the “/upload” endpoint using the POST method, and the “enctype” attribute is set to “multipart/form-data” to handle file uploads. HTML
Run the Serverrun the tornado server using below command python script_name.py Output: ConclusionTornado’s simplicity, performance, and asynchronous capabilities make it an excellent choice for developing modern web applications. Whether you are building a simple web service or a real-time application, Tornado’s flexibility and scalability can meet your requirements. With its easy-to-understand syntax and powerful features, Tornado remains a popular choice among developers for creating efficient and responsive web applications in Python. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |