- “Tornado” is a Python based framework to create web applications.
- Most important features of this framework are:-
—Asynchronous I/O calls.
—Web Sockets.
—Maximum Polling .
- In this Demo, “We will learn about installing the tornado site package with a simple program listing to a port”.
- To install tornado framework use the PIP tool.Below screenshot shows the detailed command for installation of tornado framework,
- Yo can verify the installation by checking the ‘site-packages‘ directory under python library.Below screenshot shows the installation path,
- Let’ create a python project ‘TornadoWebAppDemo‘ and have a ‘main.py‘ python file as below,
- The main.py file contains the below code,
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hi Sandeep, Welcome to Tornado Web Framework.")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
- About the above code:-
_main_ :
–This is the main method where python start executing.Single point Entry for the program.
tornado.web.Application :
–This is the class which creates a base for web application taking a collection of handlers.
–In this Demo “MainHandler” is the single handler mapped to root path.
listen(8888) :
–Application listen to port 8888.Any client can communicate this application using this port .
tornado.ioloop.IOLoop.instance().start():
–IOLoop class is main event for the application.
–Create a non blocking thread for an application.
–It is singleton in nature.
MainHandler:
–It is handler for a request.
— It refers to the current context using ‘self‘.
— Returns the response using write() method.
- We can run the application using command prompt or use any IDE like Eclipse with python or pycharm.
In Command Prompt,
In Pycharm,
- Below screen shot shows output in the browser,