tornado· 数据库连接池及定义自己的api返回json

#!/usr/bin/env python
#_*_ coding:utf-8_*_

import tornado.ioloop
import tornado.web
import tornado.escape
import pymssql,pymysql
from DBUtils.PooledDB import PooledDB


class Database:
    def __init__(self,*db):
        if len(db) == 5:
            #mysql
            self.host = db[0]
            self.port = db[1]
            self.user = db[2]
            self.pwd = db[3]
            self.db = db[4]
        else:
            #mssql
            self.host = db[0]
            self.port = None
            self.user = db[1]
            self.pwd = db[2]
            self.db = db[3]
        self._CreatePool()

    def _CreatePool(self):
        if not self.db:
            raise NameError + '没有设置数据库信息'
        if (self.port == None):
            self.Pool = PooledDB(creator=pymssql, mincached=2, maxcached=5, maxshared=3, maxconnections=6,
                                 blocking=True, host=self.host, user=self.user, \
                                 password=self.pwd, database=self.db, charset="utf8")
        else:
            self.Pool = PooledDB(creator=pymysql, mincached=2, maxcached=5, maxshared=3, maxconnections=6,
                                 blocking=True, host=self.host, port=self.port, \
                                 user=self.user, password=self.pwd, database=self.db, charset="utf8")

    def _Getconnect(self):
        self.conn = self.Pool.connection()
        cur = self.conn.cursor()
        if not cur:
            raise "数据库连接不上"
        else:
            return cur
        # 查询sql

    def ExecQuery(self, sql):
        cur = self._Getconnect()
        cur.execute(sql)
        relist = cur.fetchall()
        cur.close()
        self.conn.close()
        return relist
        # 非查询的sql

    def ExecNoQuery(self, sql):
        cur = self._Getconnect()
        cur.execute(sql)
        self.conn.commit()
        cur.close()
        self.conn.close()

gdbp = Database
class MainHadle(tornado.web.RequestHandler):
    def get(self,*args):
        filename = self.get_argument('filename')
        print(filename)
        self.set_header('Content-Type', 'application/octet-stream')
        self.set_header('Content-Disposition', 'attachment; filename=%s'%filename.encode('utf-8'))
        with open(filename,'rb') as f:
            while True:
                data = f.read(1024)
                if not data:
                    break
                self.write(data)

class MainIndex(tornado.web.RequestHandler):
    def get(self):
        self.write('Hello')

class CheckUser(tornado.web.RequestHandler):
    def get(self):
        user = self.get_argument('user')
        pwd = self.get_argument('passwd')
        #print(user)

        if user != '' and pwd != '':
            lssql =  "select usr_code,password from sb_user where usr_code= '%s' " % user
            #print(lssql)
            rds =  gdbp.ExecQuery(lssql)
            if rds[0][1] == pwd :
                js_str = tornado.escape.json_encode('{"result":"true","msg":""}')
                self.write(js_str)
            else:
                js_str = tornado.escape.json_encode('{"result":"false","msg":"用户或密码错误"}')
                self.write(js_str)
            #print(rds[0][0])
        else:
            js_str = tornado.escape.json_encode('{"result":"false","msg":"参数错误"}')
            self.write(js_str)




def make_app():
    return tornado.web.Application([(r"/download",MainHadle),(r"/",MainIndex),(r"/checkuser",CheckUser)])

def make_dbpool():
    global gdbp
    gdbp = Database('172.20.1.2','sa','xxx','MPL')


if __name__ == '__main__':
    app = make_app()
    app.listen(8888)
    make_dbpool()
    tornado.ioloop.IOLoop.current().start()




 

你可能感兴趣的:(python)