Guest User

Untitled

a guest
Dec 8th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import tornado.ioloop
  2. import tornado.web
  3. import tornado.database
  4.  
  5. class Main(tornado.web.RequestHandler):
  6. def get(self):
  7. self.write("Main")
  8.  
  9. class DBHandler(tornado.web.RequestHandler):
  10. def get(self):
  11. db = tornado.database.Connection(
  12. host="localhost", database="mydb",
  13. user="root", password="root")
  14. rows = db.query("select Id,Title from post")
  15. db.close()
  16.  
  17. top = "<html><body><b>Posts</b><br /><br />"
  18. table = "<table border=\"1\"><col width=\"50\" /><col width=\"200\" />"
  19. for row in rows:
  20. table += "<tr><td>" + str(row["Id"]) + "</td><td>" + str(row["Title"]) + "</td></tr>"
  21. bottom = "</body></html>"
  22. self.write(top+table+bottom)
  23.  
  24. application = tornado.web.Application([
  25. (r"/", Main),
  26. (r"/posts", DBHandler),
  27. ])
  28.  
  29. if __name__ == "__main__":
  30. application.listen(8888)
  31. tornado.ioloop.IOLoop.instance().start()
Add Comment
Please, Sign In to add comment