You must have observed, all this while we have written tornado based applications that listen on port 8888, and in MySQL post, we had predefined our database connection details (like server, username, password etc). But what if we need to change these details based on modules or applications or configurations? Tornado, provides this with a module tornado.options.

You could use this module in different ways for different purposes.. Lets see them in detail

Example 1:

This is a basic example where the current module define its options with define() method. For instance, port is defined as define(“port”, default=8888)

Example 2:

It’s always not good to define options for every module separately. Case for some re-usability? Yes, one could define options in a module which can be imported. In this example, all options are defined in that is imported in the main program.

Example 3:

Options can also be defined on command line which is possible via tornado.options.parse_command_line(). In the example below, we pass logging level (info|debug|…) on command line, that the module understands with parse_command_line() method.

Example 4:

Options can also be defined in a config file with tornado.options.parse_config_file(). In this example, we use to define port, host, database with username and password. We also define port in the main program. But the options for port defined in config py takes precedence and GET on http://localhost:8000/db would work but not on port 8888