示例#1
0
def regist(environ):
    method = environ.get('REQUEST_METHOD').lower()
    if method == 'post':
        try:
            request_body_size = int(environ.get('CONTENT_LENGTH', 0))
        except (ValueError):
            request_body_size = 0
        request_body = environ['wsgi.input'].read(request_body_size)
        data = parse_qs(request_body)
        user = data.get(b'user')[0].decode('utf8')
        pwd = data.get(b'pwd')[0].decode('utf8')
        if user.strip() and pwd.strip():
            mysql_check = Mysql('localhost',
                                3306,
                                'root',
                                'root',
                                dbname='oldboy',
                                charset="utf8")
            sql = "select * from user_info WHERE  name=%s"
            res = mysql_check.exec_sql(sql, user, pwd)
            if res and isinstance(res, list):
                data = b'username is already exits!'
            else:
                data = op_file('./templates/index1.html')
                sql_reg = "insert into user_info(name,password) values(%s,%s);"
                mysql_check.exec_sql(sql_reg, user, pwd)
        else:
            data = b'username and password can not be empty!'
    else:
        data = op_file('./templates/register.html')
    return data
示例#2
0
def login(environ):
    method = environ.get('REQUEST_METHOD').lower()
    if method == 'post':
        try:
            request_body_size = int(environ.get('CONTENT_LENGTH', 0))
        except (ValueError):
            request_body_size = 0
        request_body = environ['wsgi.input'].read(request_body_size)
        data = parse_qs(request_body)
        user = data.get(b'user')[0].decode('utf8')
        pwd = data.get(b'pwd')[0].decode('utf8')
        mysql_check = Mysql('localhost',
                            3306,
                            'root',
                            'root',
                            dbname='oldboy',
                            charset="utf8")
        sql = "select * from user_info WHERE  name=%s and password=%s"
        res = mysql_check.exec_sql(sql, user, pwd)
        if res and isinstance(res, list):
            data = op_file('./templates/index1.html')
        else:
            data = b'username or password is wrong!'
    else:
        data = op_file('./templates/login.html')
    return data
示例#3
0
 def get_server_id(self, server_name):
     try:
         db = Mysql.connect('ywshow')
         sql = "SELECT f_serverid serverid FROM `mo_config` WHERE f_servername=%s "
         res = db.query(sql, [server_name])
         if not res:
             print 'the server_name not find !'
         return res[0]['serverid']
     except:
         print 'some error in get_server_id: %s' % traceback.format_exc()
示例#4
0
 def handDb(self, server_id, data):
     try:
         host = get_ip_address()
         db = Mysql.connect('ywshow')
         ret = db.execute(
             "insert into mo_server (f_serverid,f_data,f_serverip,f_instime) values(%s,%s,%s,now())",
             [server_id, JsonUtil.write(data), host])
         if not ret:
             print 'insert faild !'
         self.upExtend(server_id, host)
     except:
         print 'some error in handDb: %s' % traceback.format_exc()
示例#5
0
 def upExtend(self, serverid, host):
     try:
         db = Mysql.connect('ywshow')
         res = db.query(
             "select f_serverip serverip from mo_extend where f_serverid=%s",
             [serverid])
         if not res:
             db.execute(
                 "insert into mo_extend (f_serverid,f_serverip) values ( %s, %s )",
                 [serverid, host])
         else:
             sip = res[0]['serverip'].split(',')
             if host not in sip:
                 sip.append(host)
                 sip = ','.join(sip)
                 db.execute(
                     "update mo_extend set f_serverip=%s where f_serverid=%s",
                     [sip, serverid])
     except:
         print 'some error in handDb: %s' % traceback.format_exc()
示例#6
0
		elif userBackendMethod == "MYSQL":

			backendUsername = str(configRoot.find("storage").find(
				"storageBackend").attrib["username"])
			backendPassword = str(configRoot.find("storage").find(
				"storageBackend").attrib["password"])
			backendServer = str(configRoot.find("storage").find(
				"storageBackend").attrib["server"])
			backendPort = int(configRoot.find("storage").find(
				"storageBackend").attrib["port"])
			backendDatabase = str(configRoot.find("storage").find(
				"storageBackend").attrib["database"])

			globalData.storage = Mysql(backendServer, backendPort,
				backendDatabase, backendUsername, backendPassword,
				globalData.version)

		else:
			raise ValueError("No valid storage backend method in config file.")

		# get server configurations
		globalData.serverCertFile = str(configRoot.find("general").find(
				"server").attrib["certFile"])
		globalData.serverKeyFile = str(configRoot.find("general").find(
				"server").attrib["keyFile"])
		port = int(configRoot.find("general").find("server").attrib["port"])

		if (os.path.exists(globalData.serverCertFile) is False
			or os.path.exists(globalData.serverKeyFile) is False):
			raise ValueError("Server certificate or key does not exist.")
示例#7
0
# -*- coding: utf-8 -*-
# @Time    : 2019/7/16 10:32
# @Author  : Xiao

from lib import Mysql

sql_check_db = "show databases like 'oldboy';"
sql_create_db = 'create database oldboy DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'
sql_check_table = 'show tables;'
sql_create_table = 'create table user_info(id int(10) PRIMARY KEY auto_increment,name VARCHAR(20),password VARCHAR(20),age int(10))'
sql_check_data = 'select count(*) from oldboy'
sql_create_data = "INSERT into user_info(name,password,age) values ('xg','a123456',18),('xk','a123456',38),('xh','a123456',28)"

mysql_check = Mysql('localhost', 3306, 'root', 'root', charset="utf8")

if mysql_check.exec_sql(sql_check_db):
    mysql_create = Mysql('localhost',
                         3306,
                         'root',
                         'root',
                         dbname='oldboy',
                         charset="utf8")
    if mysql_create.exec_sql(sql_check_table):
        if not mysql_create.exec_sql(sql_check_data):
            mysql_create.exec_sql(sql_create_data)
    else:
        mysql_create.exec_sql(sql_create_table)
        mysql_create.exec_sql(sql_create_data)
else:
    mysql_check.exec_sql(sql_create_db)
    mysql_create = Mysql('localhost',
示例#8
0
            configRoot.find("storage").find("mysql").attrib["server"])
        global_data.mysql_port = int(
            configRoot.find("storage").find("mysql").attrib["port"])
        global_data.mysql_database = str(
            configRoot.find("storage").find("mysql").attrib["database"])
        global_data.mysql_username = str(
            configRoot.find("storage").find("mysql").attrib["username"])
        global_data.mysql_password = str(
            configRoot.find("storage").find("mysql").attrib["password"])
        global_data.mysql_connection_retries = int(
            configRoot.find("storage").find(
                "mysql").attrib["connectionRetries"])

        global_data.logger.info("[%s]: Testing database configuration." %
                                file_name)
        storage_test = Mysql(global_data)
        if not storage_test.connection_test():
            raise ValueError("Database settings test failed.")

    except:
        global_data.logger.exception("[%s]: Could not parse config." %
                                     file_name)
        sys.exit(1)

    # Start database cleaning thread.
    global_data.logger.info("[%s]: Starting database cleaning thread." %
                            file_name)
    db_cleaner = DatabaseCleaner(global_data)
    db_cleaner.daemon = True
    db_cleaner.start()