from __future__ import print_function
import os.path
import tornado.web
import tornado.httpserver
import tornado.ioloop
from random import randrange
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/static/(.*)", StaticHandler),
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True,
autoescape=None
)
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
#called every time someone sends a GET HTTP request
@tornado.web.asynchronous
def get(self):
self.render(
"index.html",
test = randrange(1, 1000),
)
# deliver static files to page
class StaticHandler(tornado.web.RequestHandler):
def get(self, filename):
with open("static/" + filename, "r") as fh:
self.file = fh.read()
# write to page
if filename.endswith(".css"):
self.set_header("Content-Type", "text/css")
elif filename.endswith(".js"):
self.set_header("Content-Type", "text/javascript")
elif filename.endswith(".png"):
self.set_header("Content-Type", "image/png")
self.write(self.file)
try:
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()
except (KeyboardInterrupt, SystemExit):
print("\nQuit\n")