Skip to main content

Setting up your first Tornado server

Before we begin, ensure you have Python installed on your computer. You can download it from the official Python website.

This tutorial also assumes you're familiar with basic Python programming. If not, consider brushing up on your Python skills first.

Step 1: Installing Tornado

Tornado is available on PyPI and can be installed with pip, Python's package installer. Open your terminal or command prompt and type the following command:

pip install tornado

This command tells pip to download and install the Tornado package from PyPI.

Step 2: Verifying the Installation

After the installation, you can verify whether Tornado was successfully installed by trying to import it in a Python script. Open your Python IDE or command line, then type:

import tornado

If there are no error messages, congratulations! Tornado was successfully installed.

Step 3: Creating Your First Tornado Server

With Tornado installed, you're ready to create your first Tornado server. A minimal Tornado server looks like this:

import tornado.ioloop
import tornado.web

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

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

if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()

This code creates a simple Tornado web server that listens on port 8888 and responds with "Hello, world" to HTTP GET requests at the root URL ("/").

Step 4: Understanding the Code

Let's break down the code:

  1. Importing Necessary Modules: Tornado's ioloop and web modules are imported at the beginning. The ioloop module contains the core I/O loop, while web contains most of the Tornado web framework.

  2. Defining a Request Handler: A RequestHandler is defined. This is a class that handles HTTP requests. Our handler, MainHandler, overrides the get method to respond to HTTP GET requests with "Hello, world".

  3. Creating the Application: The make_app function creates an instance of tornado.web.Application, which is the Tornado web framework's main entry point. The application is configured with a list of URL patterns, each associated with a RequestHandler. In our case, we associate the root URL ("/") with MainHandler.

  4. Starting the Server: In the script's main block, we create an application, tell it to listen on port 8888, and start the I/O loop, which is the core of every Tornado application.

Step 5: Running the Server

Save your Python script, then run it from the command line:

python myscript.py

Replace myscript.py with the name of your Python script. After running this command, your Tornado server should be up and running! Open a web browser and go to http://localhost:8888. You should see "Hello, world" displayed on the page.

In this tutorial, you learned how to install Tornado, create a basic Tornado server, and run it. In the next articles, we'll dive deeper into Tornado's features and capabilities. Keep practicing and experimenting!