A simple program to begin with python tornado

Python tornado is a powerful framework for dealing with HTTP requests. It helps us to write responses to HTTP requests in a very simple and elegant way. We can write handlers for responding to the HTTP requests very easily using tornado. This is very simple to learn and use. We can create an excellent application in very few lines of code. Here I am explaining about a simple tornado application. The code is attached below. For running this code, you need tornado to be installed in your machine.

In the beginning of the code, you can see few imports related to tornado. These libraries are required for our tornado application. After that you can see a define function. This define is imported from a library called options in tornado. Using this we can get user defined arguments from commandline. Here we are defining the port in which our tornado application should run. If the user is specifying the port in the command line, it will use that value, else it will use the default value. Here I gave default value as 8888. 

The next part is a class named HelloWorldHandler. This is a class extending the tornado RequestHandler class. This is basically a handler, which means this will handle an HTTP request. This class will be called based on the navigation rules that we define in the tornado. In this class there is only one method called get(). So this handler can handle only get requests. In the get method, we are just printing a text “Someone called me” and writing a response. So whenever this class is called, The text “Someone called me” will be printed in the console and the self.write(“Welcome to Tornado..!!”) will send the this string to the HTTP response.

The next part will run the tornado application. The “tornado.web.Application(handlers=[(r”/”, HelloWorldHandler)])” defines when to invoke the handler. the r”/” is a regex. So if the url comes without any path, the request will be navigated to HelloWorldHandler class. Similar to this we can have a list of regex – handler class pairs. Here we have only one.

Execution.

python HelloTornado.py –port 9090

This will run the application in 9090 port. After this open the web browser and check http://localhost:9090. You will get a message “Welcome to Tornado.!!” on the screen. For every hit, you can see a message “Someone called me” getting printed in the console.

python HelloWorld.py

This will run the application in the default port that we specified. I specified 8888. So open the webbrowser and check http://localhost:8888.

Note: If you are executing the code in a different machine, you should use the ip address of the machine instead of localhost.

Happy Learning … 🙂

Advertisements

About amalgjose
I am an Electrical Engineer by qualification, now I am working as a Software Engineer. I am very much interested in Electrical, Electronics, Mechanical and now in Software fields. I like exploring things in these fields. I like travelling, long drives and very much addicted to music.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: