Tornado – Escape – Json

Tornado web server exports methods for escape/unescape html, xml, Json among others. This blog discusses about encoding and decoding JSON format.

Tornado has the following methods:

  • tornado.escape.json_encode(value) – JSON’ify the Python object passed to it as argument
  • tornado.escape.json_decode(value) – Converts the JSON string into Python Object

Here’s a usage example:

In this example,

1. When user browses to http://localhost:8888/blog, jsonform.html is rendered that asks for ‘Title’ and ‘Author’

2. On filling this form, a POST request is sent to /blog URL, where the posted arguments are encoded to JSON string with tornado.escape.json_encode() and rendered on the user browser

3. class Language is request handler that caters to http://localhost:8888/lang. In this class, a Python dictionary object is converted to JSON string with tornado.escape.json_encode() and responds with this JSON string to any client request.

4. When makes a GET request to /lang URL, JSON string is sent as a response. The client decodes this JSON string to a Python dictionary object with tornado.escape.json_decode() method

Code for below

Tornado – Database MySQL Client Wrapper

Tornado provides a simple MySQL wrapper for performing database operations. Class tornado.databse.Connection acts as a wrapper over MySQLdb DB-API connection.

Consider you have a MySQL installed on your system and you create a DB ‘mydb’ with table ‘post’ and records as below:

mysql> use mydb
Database changed

mysql> create table post (Id int, Title char(50), Author char(50));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into post values (1, 'Tornado Database', 'TechnoBeans');
Query OK, 1 row affected (0.01 sec)

mysql> insert into post values (2, 'Tornado Authentication', 'TechnoBeans');
Query OK, 1 row affected (0.00 sec)

mysql> insert into post values (3, 'Tornado Locale', 'TechnoBeans');
Query OK, 1 row affected (0.00 sec)

mysql> select * from post;
| Id   | Title                  | Author      |
|    1 | Tornado Database       | TechnoBeans |
|    2 | Tornado Authentication | TechnoBeans |
|    3 | Tornado Locale         | TechnoBeans |
3 rows in set (0.02 sec)

Tornado helps you to connect, retrieve and display records from ‘mydb’ MySQL DB as shown in the example below



In this example:

1. tornado.database.Connection connects to MySQL DB instance with appropriate server, database name, username and passwd

2. Object of class tornado.database.Connection ‘db’ is then used to query the records of table ‘post’

3. DB connection is then closed with db.close()

4. Records are then rendered by the webserver on accessing http://localhost/posts URL


While trying out this example, you may bump into this error

ubuntu@ubuntu:~/tornado-2.2$ python 
Traceback (most recent call last):
  File "", line 4, in 
    import tornado.database
  File "/home/ubuntu/tornado-2.2/tornado/", line 20, in 
    import MySQLdb.constants
ImportError: No module named MySQLdb.constants

This is because python-mysqldb is not installed on your system. On ubuntu systems, you can get it with

sudo apt-get install python-mysqldb

Tornado – Asynchronous Requests

Tornado is a non-blocking I/O web server. In Tornado, when the request handler serves the request made by the client, the request is closed by itself. Python decorator @tornado.web.asynchronous can be used to override the default behavior (of automatically finishing the request) and keep the request open. So, its the duty of the server developer to finish the request.


In the above example, When user browses to, AsyncHandler handles it and sends a GET request to and receives the response. Processing of this response is done by _async_callback() method. Thus in this example, when get() returns, the request has not been finished. Once the response is processed and self.finish() is called, only then request is completed.

If the developer fails to finish the request with self.finish(), the browser hangs as in the picture below.

Tornado – Error Handling

Web development often calls for gracefully handling of non-existent pages or broken links that users request for. A simple way to achieve error handling in Tornado is with:

  • tornado.web.HTTPError - Exception handler that can used to generate error code of specified type. 
    It is often used to raise an exception as we would see in the example below
  • send_error - Generates an error page of the status code provided along with error description if debug=True

In the example below, when user generates a GET request on, articles along with author names are shown up.

But when GET request is made on, since there is no Id implementation for articles page, error is handled with self.send_error(500) or raise tornado.web.HTTPError(404, ‘Page Not Found Error!!’)

Case1: raise tornado.web.HTTPError(404, ‘Page Not Found Error!!’) is enabled

  • Traceback (most recent call last):                                          —– Traceback is noticed on web page
    File “/home/ubuntu/tornado-2.2/tornado/”, line 988, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
    File “”, line 12, in get
    raise tornado.web.HTTPError(400, ‘Page Not Found Error!!’)
    HTTPError: HTTP 400: Bad Request (Page Not Found Error!!)
  • WARNING:root:400 GET /articles/1 ( Page Not Found Error!!     —– Error description where web server is running
    WARNING:root:400 GET /articles/1 ( 10.68ms

Case2: self.send_error(500) is enabled

  • ‘500: Internal Server Error’ web page shows up
  • ERROR:root:500 GET /articles/1 ( 0.41ms error line is generated where web server is running


Tornado – Templates – Run time and Cached

Website development often calls for reuse of pages. For instance, when you open your Citibank account after login the welcome page you see is same what other bank customers see but its customized with your name and settings. Do you think Citi creates so many web pages for all its customers? Well, they use, what’s called as template. Template is a layout or a skeleton that separates content from presentation and helps in mass production of web pages..

Control Statements in Templates

Tornado supports templates with expressions and control statements; straight from Tornadoweb, “Control statements are surronded by {% and %}, e.g., {% if len(items) > 2 %}. Expressions are surrounded by {{ and }}, e.g., {{ items[0] }}.”

In the above example, we have created a template (template.html) that requires a scalar value for title and a dictionary for listing dictionary items in the form of a table (List data types also work well in Tornado templates). The python code provide these values in the code line

self.render(‘template.html’, title=’Articles’, dict=articles)


tornado templates example

Run-time Template Generation and Compiling Templates

Let’s now understand two important methods in Tornado templates, how do they work and how are they implemented:

  1. Template()
  2. Loader()

Template() – helps in generating a template at run time. For instance, there is a web page with lesser content and that need not be rendered quite often, we can utilize this Template method.

Loader() – helps in compiling and caching templates from a predefined location of your machine. Compiling and caching improves performance of web server while rendering web pages.

Example 2

In example 2,

t = Template(“<html>{{ name }}</html>”) — generates template at run time

self.write(loader.load(“template.html”).generate(name=”John”))  — template is compiled, cached and loaded

Python Easter Eggs

This is something I discovered incidentally… On the Python interpreter try out these commands

ActivePython (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.

>>> import this  –> Prints out Zen of Python
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

>>> from __future__ import braces  –> Suggests there are no braces in Python (not a chance 🙂 )
File “<stdin>”, line 1
SyntaxError: not a chance

>>> import antigravity  –> opens up xkcd on Python

>>> import __hello__
Hello world…

>>> import __hello__  –> doesnt print anything, it needs to be reloaded like below

>>> reload(__hello__)  –> reloading __hello__
Hello world…
<module ‘__hello__’ from ‘<frozen>’>


5 Ways of Fibonacci in Python

After learning so much about development in Python, I thought this article would be interesting for readers and to myself…

This is about 5 different ways of calculating Fibonacci numbers in Python

## Example 1: Using looping technique
def fib(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a
print fib(5)

## Example 2: Using recursion
def fibR(n):
 if n==1 or n==2:
  return 1
 return fibR(n-1)+fibR(n-2)
print fibR(5)

## Example 3: Using generators
a,b = 0,1
def fibI():
 global a,b
 while True:
  a,b = b, a+b
  yield a

## Example 4: Using memoization
def memoize(fn, arg):
 memo = {}
 if arg not in memo:
  memo[arg] = fn(arg)
  return memo[arg]

## fib() as written in example 1.
fibm = memoize(fib,5)
print fibm

## Example 5: Using memoization as decorator
class Memoize:
 def __init__(self, fn):
  self.fn = fn
  self.memo = {}
 def __call__(self, arg):
  if arg not in self.memo:
   self.memo[arg] = self.fn(arg)
   return self.memo[arg]

def fib(n):
 a,b = 1,1
 for i in range(n-1):
  a,b = b,a+b
 return a
print fib(5)

You may ask, all this is okay, but what’s the best way? Wait for another post on performance of these… Its on the way!