Skip to content

Instantly share code, notes, and snippets.

@cessor
Created June 1, 2015 08:36
Show Gist options
  • Select an option

  • Save cessor/4621d0b202a2205e27ce to your computer and use it in GitHub Desktop.

Select an option

Save cessor/4621d0b202a2205e27ce to your computer and use it in GitHub Desktop.
Blank Tornado App
import os
import logging
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, StaticFileHandler, url
from tornado.options import define, options
from tornado import gen
define('port', default=8888)
define('debug', default=True)
define('loglevel', default='debug')
def directory(path):
return os.path.join(os.path.dirname(__file__), path)
class HelloHandler(RequestHandler):
@gen.coroutine
def get(self):
self.finish("Hello, world")
routes = [
url(r"/?", HelloHandler),
url(r'/(favicon\.ico)', StaticFileHandler, dict(path=directory('static')))
]
def configure():
# Read config file
try:
config_file = 'config.cfg'
options.parse_config_file(config_file, final=False)
except: pass
options.parse_command_line()
# Override Settings here
settings = dict(
autoreaload=True,
logging=options.loglevel,
compress_response=True,
debug=options.debug,
static_path=directory('static'),
xheaders=True,
)
return Application(routes, **settings)
def main():
# Run app
app = configure()
server = HTTPServer(app, xheaders=True)
server.bind(options.port)
server.start(1)
logging.info('Running on port %s' % options.port)
IOLoop.current().start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment