← Back to Libraries🌐 Web Development
📦

Mastering Tornado: Your Guide to Building Async Web Servers in Python

Explore the power of Tornado, an async web server for real-time Python applications, through this comprehensive tornado tutorial.

pip install tornado

Overview

What is tornado and why use it?

Key features and capabilities

Installation instructions

Basic usage examples

Common use cases

Best practices and tips

Common Use Cases

Code Examples

Getting Started with tornado

import tornado.ioloop\nimport tornado.web\n\nclass MainHandler(tornado.web.RequestHandler):\n    def get(self):\n        self.write('Hello, world')\n\nif __name__ == '__main__':\n    app = tornado.web.Application([(r'/', MainHandler)])\n    app.listen(8888)\n    tornado.ioloop.IOLoop.current().start()

Advanced tornado Example

import tornado.ioloop\nimport tornado.web\nimport tornado.websocket\n\nclass EchoWebSocket(tornado.websocket.WebSocketHandler):\n    def open(self):\n        print('WebSocket opened')\n\n    def on_message(self, message):\n        self.write_message(u'You said: ' + message)\n\n    def on_close(self):\n        print('WebSocket closed')\n\nif __name__ == '__main__':\n    app = tornado.web.Application([(r'/ws', EchoWebSocket)])\n    app.listen(8888)\n    tornado.ioloop.IOLoop.current().start()

Alternatives

Common Methods

IOLoop.current

Returns the current thread's IOLoop.

More Web Development Libraries