/gist:e30b33abeee31bc4b7c7 Secret
Last active
August 29, 2015 14:01
-
-
Save minrk/e30b33abeee31bc4b7c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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