Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created August 22, 2012 08:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cjgiridhar/3423802 to your computer and use it in GitHub Desktop.
Tornado - MySQL DB API
import tornado.ioloop
import tornado.web
import tornado.database
class Main(tornado.web.RequestHandler):
def get(self):
self.write("Main")
class DBHandler(tornado.web.RequestHandler):
def get(self):
db = tornado.database.Connection(
host="localhost", database="mydb",
user="root", password="root")
rows = db.query("select Id,Title from post")
db.close()
top = "<html><body><b>Posts</b><br /><br />"
table = "<table border=\"1\"><col width=\"50\" /><col width=\"200\" />"
for row in rows:
table += "<tr><td>" + str(row["Id"]) + "</td><td>" + str(row["Title"]) + "</td></tr>"
bottom = "</body></html>"
self.write(top+table+bottom)
application = tornado.web.Application([
(r"/", Main),
(r"/posts", DBHandler),
])
if __name__ == "__main__":
application.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