Tornado is a open-source, fast, scalable non-blocking web server used for development of real time web services. It solves the c10k problem by using epoll library and can handle thousands of simultaneous connections. It runs as a single threaded non-blocking io loop that waits on requests (events) from users and servers them (hence also known as event-triggered web server). Unlike blocking web servers where thread management, context switching becomes expensive, Tornado gains in thread safety.
See below for prerequisites, installation steps along with an example of Hello World program in Tornado. Don’t worry about many things right now, just see how Tornado web server runs and realize the ease of web development with Tornado! 🙂
This post would be followed by other posts detailing about the capabilities of Tornado web framework. Please note: We use, Ubuntu 10.04, Python v2.6 and tornado2.2 for all the posts.
Prerequisites
Python 2.5+ PycURL for python v2.6 For other python version you could visit tornado
Installation
tar xvzf tornado-2.x.tar.gz
cd tornado-2.x
python setup.py build
sudo python setup.py install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Torando Web Server hello World | |
import tornado.ioloop | |
import tornado.web | |
class Hello(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Hello, world") | |
application = tornado.web.Application([ | |
(r"/", Hello), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Request to Torando Web server Hello World | |
import httplib2 | |
h = httplib2.Http() | |
resp, content = h.request("http://127.0.0.1:8888", "GET") | |
print "Reponse:", resp, "\nContent:", content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reponse: {'status': '200', 'content-length': '12', 'content-location': 'http://127.0.0.1:8888', 'server': 'TornadoServer/2.2', | |
'etag': '"e02aa1b106d5c7c6a98def2b13005d5b84fd8dc8"', 'content-type': 'text/html; charset=UTF-8'} | |
Content: Hello, world |