Skip to content

Instantly share code, notes, and snippets.

@minrk
Forked from jamesgao/gist:23395fb043d5dd939ee1
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/e30b33abeee31bc4b7c7 to your computer and use it in GitHub Desktop.
Save minrk/e30b33abeee31bc4b7c7 to your computer and use it in GitHub Desktop.
import os
import struct
import threading
from tornado import ioloop
import tornado.web
from tornado import httpserver
class MainHandler(tornado.web.RequestHandler):
def get(self, path):
self.write("Hello, world")
class WebApp(object):
def __init__(self, port=9494):
self.port = port
self.server = None
self.ioloop_thread = None
def setup(self):
"""set up the application
could just as easily be done in init
"""
application = tornado.web.Application([(r"/(.*)", MainHandler)])
self.server = httpserver.HTTPServer(application)
self.server.listen(self.port)
def start_loop(self):
"""Start the IOLoop in a background thread
only if it isn't running already
"""
io_loop = ioloop.IOLoop.instance()
if not io_loop._running:
# if a loop is not already running,
# start it in a background thread
self.ioloop_thread = thread = threading.Thread(target=io_loop.start)
thread.start()
if __name__ == "__main__":
app = WebApp()
app.setup()
app.start_loop()
import webbrowser
webbrowser.open("http://localhost:%i" % app.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment