Python Web Framework

Quick Review of Powerful Asynchronous Python Web Framework: Tornado Web Server

Web Frameworks

Tornado Web Server is a Python web framework as well as asynchronous networking library. Asynchronous means scaling up multiple connection to save memory. It makes thousands of connection ideal for web sockets. Before proceeding further, let me talk about web sockets. These are defined as bidirectional communication between the client and server where connection is alive until terminated by the client. It helps in keeping simultaneous connection open at the same time.

Tornado is not based on WSGI?

Before pointing to the solution to this question, let me tell you what is WSGI. It is Web Server Gateway Interface. Considering Django as an application, it acts as interface between server and Django application. There are many servers which support WSGI such as flup, Gunicorn, James and mod_wsgi. I have just pointed out some of the example. List is large. WSGI is a synchronous interface while tornado concurrent model is based on single threaded asynchronous execution.

Tornado is a non blocking server. What is this?

There are two types of servers blocking and non-blocking. Let us go through the basics of these both. Blocking web servers can be treated as handling the queue at the post office. Key concept is that until work of one person is not completed it will not process request of next person in queue. It operate in synchronous mode whereas Non-Blocking web server works in asynchronous mode where processes are handled by a single thread. While processing a request, it takes multiple request. Once the previous request is handled it starts working on the next request without keeping any waiting time. All such is achieved by a concept called event loop. Tornado is a non blocking web server. Hope you understand it well.

Installation:

pip install tornado

Code Example:

Let me show you how to display the content on web using Tornado using classes such as RequestHandler and object such as Application to hit url.

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)
   print("Please listen to port no 8888")
   tornado.ioloop.IOLoop.current().start()

This is just a simple program and before let see the output of this program when you type on web browser 127.0.0.1:8888 you will get the following response

Hitting the Result on Web Local Host
Hitting the Result on Web Local Host

Some Elaboration of the above code:

We have created a class Hello and inside it another function get which will display “Hello, world” on web browser. We have used another object Application to hit the result at url provided there. So overall we need the content to get displayed and url where it will be displayed which is accomplished using RequestHandler class and Application Object.

Let us build some more example by integrating HTML with Tornado

I will use the html code and render it using Tornado. Please see below codes, one is python code and another is HTML Code

File1: Python file where we have rendered html file which I am showing in the next code

import tornado.ioloop
import tornado.web
 
class Hello(tornado.web.RequestHandler):
    def get(self):
        self.render("new.html")
 
application = tornado.web.Application([
    (r"/", Hello), ])
 
if __name__ == "__main__":
    application.listen(8888)
    print("Please listen to port no 8888")
    tornado.ioloop.IOLoop.current().start()

 

HTML File: new.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Self Awareness</title>
</head>
<body>
<h1>Self Awareness hub is an awesome place to visit</h1>
</body>
</html>

 

Please refer to the below Screenshot to See the result when you hit 127.0.0.1:8888 on web

Rendering HTML with Tornado
Rendering HTML with Tornado

Conclusion:

Python has various web framework such as Django, Flask and Tornado. Choosing a web framework is all about requirement of the developer, client or end user. This post is all about the basics of tornado and guide for displaying the content on web browser using it. It is very easy to use and is quite helpful to create API like chat bot API can be created using it. To learn more about AI chat bot please visit these links. Moreover, you can visit our parent company to know more about AI Related products such as Real time Face recognition, AI chat bot, Text based Sentimental Analysis.  Please visit www.aisangam.com (Parent company of Selfawarenesshub.org).

Introduction of Chatbot || Types of Chatbot - Part-1

Chatbot Data preparation || Tensorflow deep-learning training - Part-2

Chatbot Implementation Using Deep-Learning Tensorflow - Part-3

Chatbot Deployment on webpage GUI Flask PHP JavaScript - Part-4

Add Comment

Click here to post a comment

More Posts

Get your Language »