From 95b94a9d285b8b67adb366e0fef849440e24c4de Mon Sep 17 00:00:00 2001 From: sidya82 Date: Thu, 27 Feb 2014 15:55:16 +0100 Subject: [PATCH] =?UTF-8?q?=09renomm=C3=A9:=20=20=20=20=20=20=20=20=20lol.?= =?UTF-8?q?html=20->=20site/index.html=20=09modifi=C3=A9:=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20superTornado.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lol.html => site/index.html | 0 superTornado.py | 89 ++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 12 deletions(-) rename lol.html => site/index.html (100%) diff --git a/lol.html b/site/index.html similarity index 100% rename from lol.html rename to site/index.html diff --git a/superTornado.py b/superTornado.py index 3ecb891..f12da6a 100644 --- a/superTornado.py +++ b/superTornado.py @@ -1,22 +1,87 @@ +import markdown +import os.path +import re +import tornado.auth +import tornado.database import tornado.httpserver import tornado.ioloop +import tornado.options +import tornado.web +import unicodedata + +from tornado.options import define, options + +define("port", default=80, help="run on the given port", type=int) +define("mysql_host", default="127.0.0.1", help="api database host") +define("mysql_database", default="tornado_api", help="tornado_api database name") +define("mysql_user", default="root", help="tornado_api database user") +define("mysql_password", default="", help="tornado_api database password") +class Application(tornado.web.Application): + def __init__(self): + project_dir = './site' + + handlers = [ + (r"/all_book/", BooksHandler), + (r"/all_category/", CategoryHandler), + ] + settings = dict( + #autoescape=None, + ) + tornado.web.Application.__init__(self, handlers, **settings) + + # Have one global connection to the blog DB across all handlers + self.db = tornado.database.Connection( + host=options.mysql_host, database=options.mysql_database, + user=options.mysql_user, password=options.mysql_password) +class BaseHandler(tornado.web.RequestHandler): + @property + def db(self): + return self.application.db + + +class BooksHandler(BaseHandler): + def post(self): + try: + print "Adding new book" + name = self.get_argument("name") + title = self.get_argument("title") + author = self.get_argument("author") + if not name or not title or not author: + return self.write({"success":False}) + if not len(name) or not len(title) or not len(author): + return self.write({"success":False}) + print "[ NEW BOOK ] name ",name," title ",title," author ",author + self.db.execute( + "INSERT INTO book (name,title,author) VALUES (%s,%s,%s)",name, title,author) + self.write({"success":True}) + except: + self.write({"success":False}) + +class CategoryHandler(BaseHandler): + + def post(self): + try: + print "Adding new category" + name = self.get_argument("name") + if not name or not len(name): + return self.write({"success":False}) + print "[ NEW CATEGORY ] name ",name + self.db.execute( + "INSERT INTO category (name) VALUES (%s)",name) + self.write({"success":True}) + except: + self.write({"success":False}) + +def main(): + tornado.options.parse_command_line() + http_server = tornado.httpserver.HTTPServer(Application()) + http_server.listen(options.port) + tornado.ioloop.IOLoop.instance().start() -def main(port = 80): - ioloop = tornado.ioloop.IOLoop.instance() - application = tornado.web.Application([ - (r"/get/(sysinfo|cpuinfo)", InfoHandler) - ]) - http_server = tornado.httpserver.HTTPServer(application) - http_server.listen(port) - try: - ioloop.start() - except KeyboardInterrupt: - pass if __name__ == "__main__": main() -