Combined Routes Example - Tornado Typing CST Test
Loading…
Combined Routes Example — Tornado Code
Logging, JSON echo, and counter in a single Tornado app.
import tornado.ioloop
import tornado.web
import json
count = 0
class LogHandler(tornado.web.RequestHandler):
async def prepare(self):
print(f'{self.request.method} {self.request.uri}')
class EchoHandler(LogHandler):
async def post(self):
data = json.loads(self.request.body)
self.write(data)
class CounterHandler(LogHandler):
async def get(self):
self.write({'count': count})
async def post(self, action):
global count
if action == 'increment': count += 1
elif action == 'decrement': count -= 1
elif action == 'reset': count = 0
self.write({'count': count})
app = tornado.web.Application([
(r'/echo', EchoHandler),
(r'/counter', CounterHandler),
(r'/counter/(increment|decrement|reset)', CounterHandler)
])
if __name__ == '__main__':
app.listen(8888)
tornado.ioloop.IOLoop.current().start()Tornado Language Guide
Tornado is a Python web framework and asynchronous networking library, designed for handling thousands of simultaneous connections. It excels at real-time web services and long-lived network connections.
Primary Use Cases
- ▸Real-time chat applications
- ▸WebSocket-based dashboards
- ▸High-concurrency APIs and services
- ▸Long-polling or streaming data endpoints
- ▸IoT backends and notification services
Notable Features
- ▸Asynchronous, non-blocking I/O
- ▸Integrated web framework and networking library
- ▸Native WebSocket support
- ▸High concurrency for thousands of clients
- ▸Flexible request handling with coroutines
Origin & Creator
Tornado was created by FriendFeed engineers (Brett Slatkin, David Cournapeau, and others) in 2009, later maintained by Facebook and the open-source community.
Industrial Note
Tornado is preferred for real-time web services, WebSocket servers, and high-concurrency applications where traditional WSGI frameworks may struggle.