Tornado is a Python web framework and asynchronous networking library, that uses non-blocking network I/O which allows it to scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.
# hello_server.py
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([ (r"/", MainHandler), ]) # URL Mapping
if __name__ == "__main__":
app = make_app()
app.listen(8888) # Port Number
tornado.ioloop.IOLoop.current().start()
This App is run by typing python3 hello_server.py
or python hello_server.py
depending on the version of Python being used.
When run locally the server can be accessed by going to 127.0.0.1:8888
from the browser.
The server will return "Hello World".
In make_app()
function, the root /
is mapped to MainHandler
. This means that requests to the root IP 127.0.0.1:8888
will be mapped to the MainHandler
function.
Python3 - sudo pip3 install tornado
Python2 - sudo pip install tornado
Packages which will are optional but recommended to install alongside Tornado :