Tornado – Options

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)


from tornado.options import define, options
import tornado.web
import tornado.database
define("host", default="localhost", help="DB host")
define("database", default="mydb", help="DB used")
define("user", default="root", help="DB username")
define("password", default="root", help="DB Password")
define("port", default=8888)
class DBHandler(tornado.web.RequestHandler):
def get(self):
db = tornado.database.Connection(options.host, options.database, options.user, options.password)
db.close()
application = tornado.web.Application([
(r"/db", DBHandler),
], debug=True)
if __name__ == '__main__':
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

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 opts.py that is imported in the main program.


from tornado.options import options,define
define("host", default="localhost", help="DB host")
define("database", default="mydb", help="DB used")
define("user", default="root", help="DB username")
define("password", default="root", help="DB Password")
define("port", default=8888)

view raw

opts.py

hosted with ❤ by GitHub


from tornado.options import define, options
import tornado.web
import tornado.database
import opts
class DBHandler(tornado.web.RequestHandler):
def get(self):
db = tornado.database.Connection(options.host, options.database, options.user, options.password)
db.close()
application = tornado.web.Application([
(r"/db", DBHandler),
], debug=True)
if __name__ == '__main__':
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

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.


from tornado.options import define, options
import tornado.web
import tornado.database
define("port", default="8888")
class DBHandler(tornado.web.RequestHandler):
def get(self):
self.write("Main")
application = tornado.web.Application([
(r"/db", DBHandler),
], debug=True)
if __name__ == '__main__':
tornado.options.parse_command_line()
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

Example 4:

Options can also be defined in a config file with tornado.options.parse_config_file(). In this example, we use config.py 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


port="8000"
host="localhost"
databse="mydb"
user="root"
password="root"

view raw

config.py

hosted with ❤ by GitHub


from tornado.options import define, options
import tornado.web
import tornado.database
define("port", default="8888")
class DBHandler(tornado.web.RequestHandler):
def get(self):
self.write("Main")
application = tornado.web.Application([
(r"/db", DBHandler),
], debug=True)
if __name__ == '__main__':
tornado.options.parse_config_file("config.py")
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

3 thoughts on “Tornado – Options

  1. With Tornado options module, you can set the log level with –logging=info or debug or warning on the command line..

  2. After looking over a handful of the blog posts on your website, I honestly appreciate your way of
    writing a blog. I bookmarked it to my bookmark webpage list and will be checking back
    in the near future. Please visit my website as well and let me know your
    opinion.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.