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

Difference between / and // division in python

I have seen people using / and // for division in python. So for checking the difference I checked both of this in my system. I used python 2..7 for testing. I got same output for both the divisions. The reason for the same output is explained below.

Eg: 7 / 2   and 7 // 2  will give the same answer 3 in python 2.x

In Python 3.0, 7 / 2  will return 3.5 and 7 // 2  will return 3. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers. ie we will get 3 for both the divisions. We can enable the 3.x behavior by doing simple import.

from __future__ import division,

This causes Python 2.x to adopt the behavior of 3.0. The future import should be placed at the top of your program.

Python code for calculating the difference between two time stamps

I was searching for a way to find the difference between two timestamps. My requirement is to get the difference in terms of years, months, days, hours and minutes. I found a way to get it. The below code contains the logic to get the required output. I haven’t seen this code anywhere in internet, that is the reason I am posting this here so that this will be helpful for someone.

A Small experiment using GPS module

Full form of GPS is global positioning system. GPS is one of the very superb inventions that help us very much. GPS finds the position by making satellites as reference. It is accurate upto some meters. Now a days almost everyone in the world are using gps for navigation. Here I am sharing my experience with a gps module. GPS modules are available in electronic shops. The picture of a gps module is shown below.

gps1The gps unit that I bought is having an RS232 as well as a TTL interface. We can connect this device to a computer, raspberry pi or any micro-controller very easily. I connected and did the experiments using this device on raspberry pi, laptop and micro-controller. Here I am explaining a sample python program that will find the coordinates using this gps module. I used a python library called pynmea for reading and parsing the data returned by the gps module.

First power up the gps module. This depends on the type of gps module that you are using. The module I used supports 12V and 5V input voltage. Then connect the gps module to your computer using the RS232. If your laptop or PC is not having the RS232 port, don’t worry, you can use an RS232 to USB converter cable. Check the serial port in which this device is connected (This can be found from the device manager). Before we start writing the program, ensure that the pynmea library is installed in your computer.

First we will check whether the device is working properly. So we will write a small python program to read the data from the serial port.

__author__ = 'Amal G Jose'

import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM5'
ser.open()
while True:
    print ser.readline()

In my case the device was connected to COM5 port. Change the port accordingly and execute this program. If you are getting values in the console, then you are good to go.

Then execute the following program to parse the data and find the coordinates.

HadoopLens Toolkit – An end to end solution for fitting hadoop in an enterprise

Hadoop Lens Toolkit is a product from Knowledge Lens Pvt Limited which is an end to end solution for hadoop cluster/data center migration. This is a very useful toolkit required for any customer who is using hadoop. This is an end to end solution for hadoop cluster back up, data compression, migration and recovery. HadoopLens toolkit has the solution for all the problems/drawbacks of hadoop while implementing in an enterprise.

Some keys do not produce the correct characters in Raspberry Pi

I have a raspberry pi B+ with raspbian OS installed. In this set up, most of the keys were not producing the proper characters. Among the characters, ‘|’ was the character that I missed a lot, because I was not able to do any grep in the linux command line. I tried re-configuring the keyboard, but that didn’t helped me. Then I found the solution.

I changed the keyboard layout to US.

The command for doing the same is given below.

setxkbmap -layout us

If the above solution is not working, you can manually edit the following file and change the keyboard layout. Change the layout to US (use small letter).

/etc/default/keyboard