Created
May 13, 2015 13:12
-
-
Save hondajojo/a50a5e2ac06a1980db1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
#import pymongo | |
#import MySQLdb | |
import torndb | |
from tornado.options import define,options | |
define("port",default=8001,help='run on the given port',type=int) | |
define('mysql_host',default='127.0.0.1:3306',help='db host') | |
define('mysql_database',default='qiwsirtest',help='db name') | |
define('mysql_user',default='root',help='db user') | |
define('mysql_password',default='1',help='db password') | |
TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "templates") | |
STATIC_PATH = os.path.join(os.path.dirname(__file__), "static") | |
class Application(tornado.web.Application): | |
def __init__(self): | |
handlers = [ | |
(r'/',registerHandler), | |
(r'/register',indexHandler) | |
] | |
settings = dict( | |
template_path = TEMPLATE_PATH, | |
static_path = STATIC_PATH, | |
debug = True, | |
) | |
tornado.web.Application.__init__(self,handlers,**settings) | |
self.db = torndb.Connection( | |
host = options.mysql_host, | |
database = options.mysql_database, | |
user = options.mysql_user, | |
password = options.mysql_password, | |
) | |
class indexHandler(tornado.web.RequestHandler): | |
def get(self): | |
db = self.application.db | |
#first = db.query('select * from users') | |
#print first | |
self.render('register.html') | |
class registerHandler(tornado.web.RequestHandler): | |
def post(self): | |
name = self.get_argument('username') | |
passwd = self.get_argument('password') | |
email = self.get_argument('email') | |
db =self.application.db | |
check_result = self.check(name) | |
#print check_result | |
if check_result: | |
self.render('shouye.html') | |
#self.render('index.html') | |
else: | |
db.execute("insert into users (username,password,email) values (%s,%s,%s)",name,passwd,email) | |
self.render('index.html',name=name) | |
#db.execute("insert into users (username,password,email) values (%s,%s,%s)",name,passwd,email) | |
#self.redirect('/') | |
#self.redirect('/') | |
def check(self,name): | |
db = self.application.db | |
all = db.query('select * from users') | |
if name in [i['username'] for i in all]: | |
return True | |
return False | |
if __name__ == "__main__": | |
tornado.options.parse_command_line() | |
http_server = tornado.httpserver.HTTPServer(Application()) | |
http_server.listen(options.port) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment