Skip to content

Instantly share code, notes, and snippets.

@acgotaku
Created February 17, 2014 01:37
Show Gist options
  • Save acgotaku/9043235 to your computer and use it in GitHub Desktop.
Save acgotaku/9043235 to your computer and use it in GitHub Desktop.
a simple tornado start frame.
#!/usr/bin/env python2
import os
import logging
import tornado.options
import tornado.web
from tornado.options import define, options
define("bind", default="127.0.0.1", help="addrs that debugger bind to")
define("port", default=8888, help="run on the given port")
define("config", default="", help="config file")
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", HomeHandler),
]
settings = dict(
blog_title=u"Tornado Example",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
autoescape=None,
)
tornado.web.Application.__init__(self, handlers, **settings)
class BaseHandler(tornado.web.RequestHandler):
application_export = set(())
def __getattr__(self, key):
if key in self.application_export:
return getattr(self.application, key)
super(BaseHandler, self).__getattr__(key)
def render_string(self, template_name, **kwargs):
if "options" not in kwargs:
kwargs["options"] = options
return super(BaseHandler, self).render_string(template_name, **kwargs)
class HomeHandler(BaseHandler):
def get(self):
self.write("Hello World!");
def main():
from tornado.httpserver import HTTPServer
tornado.options.parse_command_line()
if options.config:
tornado.options.parse_config_file(options.config)
tornado.options.parse_command_line()
http_server = HTTPServer(Application(), xheaders=True)
http_server.bind(options.port, options.bind)
http_server.start()
logging.info("http server started on %s:%s" % (options.bind, options.port))
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment