Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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