Skip to content

Instantly share code, notes, and snippets.

@Shu-Ji
Created February 26, 2015 06:37
Show Gist options
  • Save Shu-Ji/3954f13e2c26fa813915 to your computer and use it in GitHub Desktop.
Save Shu-Ji/3954f13e2c26fa813915 to your computer and use it in GitHub Desktop.
nw-demo-app.py
# coding: u8
import os.path as osp
import sys
root = osp.dirname(osp.abspath(__file__))
libs = osp.join(root, 'libs')
sys.path.insert(0, libs)
import tornado.httpserver
import tornado.ioloop
from tornado.options import define, options
from tornado.util import ObjectDict
import tornado.web
define('host', default='127.0.0.1', type=str)
define('port', default=9527, type=int)
define('killnwdemo', default='killnwdemo', type=str) # for ps aux | grep
tornado.options.parse_command_line()
class BaseHandler(tornado.web.RequestHandler):
u"""Base handler"""
class MainHandler(BaseHandler):
def get(self):
self.write('index')
class PingHandler(BaseHandler):
u"""Used for telling node.js that tornado server has been started."""
def get(self):
self.write('/nwdemo/pong')
class Application(tornado.web.Application):
def __init__(self):
urls = [
(r'/', MainHandler),
(r'/nwdemo/ping', PingHandler),
]
settings = ObjectDict(debug=True)
tornado.web.Application.__init__(self, urls, settings)
def main():
h, p = options.host, options.port
tornado.httpserver.HTTPServer(Application(), xheaders=True).listen(port=p)
print('http://%s:%s' % (h, p))
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