13
Apr

The Tornado Web Server


Tornado is a web server built by Friend Feed. The framework is distinct from most mainstream web server frameworks (and certainly most Python frameworks) because it is non-blocking and reasonably fast. Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services. Tornado is also one of the few web servers built to address the C10K problem . If you have used other python frameworks like web.py or Google's webapp then you are going to feel right at home.

Installation

You will need to install the following before you install Tornado

sudo apt-get install python-dev python-pycurl python-simplejson

Once the installation is complete you can install Tornado by pasting the following into the terminal.

wget http://www.tornadoweb.org/static/tornado-0.2.tar.gz
tar xvzf tornado-0.2.tar.gz
cd tornado-0.2
python setup.py build
sudo python setup.py install

helloworld.py using Tornado

# helloworld.py
 
# Import the tornado framework
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
 
from tornado.options import define, options
 
# Define a default port for the service to run on
define("port", default=8888, help="run on the given port", type=int)
 
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")
 
 
def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
 
 
if __name__ == "__main__":
    main()

Run it terminal by issuing the following command

python helloworld.py 

or

python helloworld.py --port=8080

If all goes well you can open up a browser and visit http://localhost:8888

Follow up: How to run tornado in production

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options