Tornado Utilities in Python

Below are some of the examples of Tornado Utilities in Python:

  • Automatic Code Reloading
  • Command-Line Options
  • Logging Support

Automatic Code Reloading

In this example, in below code Tornado web app, set to run on port 8888, employs automatic code reloading. The `MainHandler` responds to GET requests with a greeting. With `tornado.autoreload`, changes in “app.py” trigger automatic server refresh, streamlining development.

Python3
import tornado.ioloop
import tornado.web
from tornado.autoreload import start, watch


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, Tornado with Automatic Code Reloading!")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    # Start monitoring for code changes
    watch("app.py")
    # Start the Tornado I/O loop with automatic reloading enabled
    start()
    tornado.ioloop.IOLoop.current().start()

Output

Command-Line Options

In this example, below Tornado code defines a simple web application with command-line options. It utilizes Tornado’s `options` module to declare and parse options like the server port and debug mode. The main handler responds with a greeting message. The script allows customization of options via the command line and starts the Tornado server.

Python3
import tornado.ioloop
import tornado.web
from tornado.options import define, options, parse_command_line

# Define options
define("port", default=8888, help="run on the given port", type=int)
define("debug", default=False, help="enable debug mode", type=bool)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, Tornado with Options!")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ], debug=options.debug)


if __name__ == "__main__":
    # Parse command-line options
    parse_command_line()

    # Create the Tornado application
    app = make_app()

    # Start the Tornado server
    app.listen(options.port)
    print(f"Server started on port {options.port}")

    # Start the I/O loop
    tornado.ioloop.IOLoop.current().start()

Output

Logging Support

In this example, This Tornado web application, set to run on port 8888, includes a `MainHandler` responding to GET requests with a greeting. The `make_app` function configures the application, and `enable_pretty_logging()` enhances log output in the terminal. The server startup is logged, and the Tornado I/O loop initiates for seamless execution.

Python3
import logging
import tornado.ioloop
import tornado.web
from tornado.log import enable_pretty_logging


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, Tornado with Logging Support!")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    # Enable colored log output in the terminal
    enable_pretty_logging()
    logging.info("Tornado server starting on port 8888...")
    tornado.ioloop.IOLoop.current().start()

Output



Python Tornado Utilities

Tornado-Utilities takes center stage, providing an arsenal of tools and features within the Tornado web framework. This article delves into the syntax-centric aspects of Tornado-Utilities, elucidating how these tools facilitate a smoother and more efficient web development process.

Similar Reads

Tornado Utilities Syntax in Python

Tornado-Utilities comprises diverse modules and classes, each designed with a specific purpose to simplify common development tasks. Let’s explore the key functionalities through a syntax-centric lens....

Tornado Utilities in Python

Below are some of the examples of Tornado Utilities in Python:...