Created
July 3, 2011 20:18
-
-
Save ncweinhold/1062572 to your computer and use it in GitHub Desktop.
Dabbling with Tornado
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 sqlite3 | |
import os.path | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.web | |
class TestApplication(tornado.web.Application): | |
def __init__(self): | |
handlers = [ | |
(r"/", IndexHandler) | |
] | |
settings = dict( | |
template_path = os.path.join(os.path.dirname(__file__), "templates") | |
) | |
tornado.web.Application.__init__(self, handlers, **settings) | |
self.db_connection = sqlite3.connect("./test.db") | |
class BaseHandler(tornado.web.RequestHandler): | |
@property | |
def db_connection(self): | |
return self.application.db_connection | |
class IndexHandler(BaseHandler): | |
def get(self): | |
cursor = self.db_connection.execute("select * from testdata") | |
entries = cursor.fetchall() | |
self.render("index.html", entries=entries) | |
if __name__ == "__main__": | |
http_server = tornado.httpserver.HTTPServer(TestApplication()) | |
http_server.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment