Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Add Comment
Please, Sign In to add comment