From 77d607f872494846eaea094ed726b58738abd63b Mon Sep 17 00:00:00 2001 From: sidya82 Date: Thu, 27 Feb 2014 16:06:38 +0100 Subject: [PATCH] =?UTF-8?q?=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20supe?= =?UTF-8?q?rTornado.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- superTornado.py | 89 +++++-------------------------------------------- 1 file changed, 8 insertions(+), 81 deletions(-) diff --git a/superTornado.py b/superTornado.py index f12da6a..67a130e 100644 --- a/superTornado.py +++ b/superTornado.py @@ -1,87 +1,14 @@ -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() +class MainHandler(tornado.web.RequestHandler): + def get(self): + self.write("Hello, world") +application = tornado.web.Application([ + (r"/", MainHandler), +]) if __name__ == "__main__": - main() + application.listen(8888) + tornado.ioloop.IOLoop.instance().start()