Tornado - MySQL DB API
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 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