def __init__(self): self.sqlhelper = MySQLHelper() self.ip = '' self.port = 0 self.user = '' self.password = '' self.gid = 0
def checkLogin(uname, password): sqlHelper = MySQLHelper() sql = 'select * from admin where uname=%s and password=%s;' params = (uname, password) reCount = sqlHelper.GetOne(sql, params) if not reCount: print "用户名或密码输错,又或者没有这个用户" else: print "认证成功"
class Admin(object): def __init__(self): self.__helper = MySQLHelper() def get_one(self, id): sql = "select * from admin where id = %s" params = (id,) return self.__helper.get_one(sql, params) def check_validate(self, username, password): sql = "select * from admin where username = %s and password = %s" params = (username, password) return self.__helper.get_one(sql, params)
class Groups_host(object): def __init__(self,uid): self.uid = uid self.sqlhelper = MySQLHelper() self.gname = '' self.gnew_name = '' def get_group(self): #查询指定uid的主机组 sql = 'select * from groups_host where uid=%s;' params = (self.uid,) result = self.sqlhelper.get_all(sql, params) return result #返回结果 def select_group(self,gname): #查询指定组名的主机组 self.gname = gname sql = 'select * from groups_host where gname=%s;' params = (self.gname,) result = self.sqlhelper.get_all(sql, params) return result #返回结果 def add_group(self,gname): #添加新主机组 self.gname = gname sql = 'insert into groups_host(gname,uid) values(%s,%s);' params = (self.gname,self.uid) result = self.sqlhelper.modify(sql, params) #修改 return result def del_group(self,gname): #删除指定主机组 self.gname = gname sql = 'delete from groups_host where gname=%s;' params = (self.gname,) result = self.sqlhelper.modify(sql, params) #修改 return result def rename_group(self,gname,gnew_name): #修改主机组的组名 self.gname = gname self.gnew_name = gnew_name sql = 'update groups_host set gname=%s where gname=%s;' params = (self.gnew_name,self.gname) result = self.sqlhelper.modify(sql, params) return result
class Admin(object): #model层定义了index能使用的各种功能,方法,并去helper里的内容 def __init__(self): self.__helper = MySQLHelper() def Get_One(self, id): sql = "select * from UserInfo where id=%s" params = (id, ) return self.__helper.Get_One(sql, params) def Checkvalidata(self, username, password): sql = "select * from UserInfo where name=%s and password=%s" params = ( username, password, ) return self.__helper.GetOne(sql, params)
def check_login(): #验证用户名和密码的正确性 error_count = 0 sql_helper = MySQLHelper() while error_count < 3: #最多只能输入三次 user = raw_input('输入用户名:') password = md5_password(raw_input('输入密码:')) sql = 'select * from admin where uname=%s and password=%s;' params = (user,password) result = sql_helper.get_all(sql, params) #接收sql语句查询的结果 if result: print '登录成功' return result[0]['uid'] #返回登录uid break else: print '用户名或者密码输错,请重新输入.' error_count += 1
class Hosts(object): def __init__(self): self.sqlhelper = MySQLHelper() self.ip = '' self.port = 0 self.user = '' self.password = '' self.gid = 0 def get_all(self, gid): #获取指定gid的所有主机 self.gid = gid sql = 'select * from hosts where gid=%s;' params = (self.gid, ) result = self.sqlhelper.get_all(sql, params) return result def select_group_host(self, gid): #查询指定组的主机ip self.gid = gid sql = 'select ip from hosts where gid=%s;' params = (self.gid, ) result = self.sqlhelper.get_all(sql, params) return result def add_host(self, ip, port, user, password, gid): #增加主机 self.ip = ip self.port = port self.user = user self.password = password self.gid = gid sql = 'insert into hosts(ip,port,user,password,gid) values(%s,%s,%s,%s,%s);' params = (self.ip, self.port, self.user, self.password, self.gid) result = self.sqlhelper.modify(sql, params) return result def del_host(self, ip, gid): #根据ip和gid,删除指定主机 self.ip = ip self.gid = gid sql = 'delete from hosts where ip=%s and gid=%s;' params = (self.ip, self.gid) result = self.sqlhelper.modify(sql, params) return result
class UsersLog(object): def __init__(self): self.__helper = MySQLHelper() def check_validate(self, username, password): """Check input is or not in Database. If is in, return (username, password); if not in, return None. """ sql = "select username, password from users_log_table where username = %s and password = %s" params = (username, password) return self.__helper.fetch_one(sql, params) def registration(self, username, password): """Add user's register information to Database. If Database doesn't have the relative table, create it first. """ sql = "insert into users_log_table(username, password) values(%s, %s)" params = [(username, password)] # 判断表是否存在 tables_list = self.__helper.fetch_all("show tables") tables = [] for table in tables_list: tables.append(table["Tables_in_pymysql"]) if "users_log_table" not in tables: sql_1 = """create table users_log_table( id int not null primary key auto_increment, username varchar(20) not null, password varchar(30) not null default "666666", e_mail varchar(30), ID_number varchar(18) ) """ self.__helper.create_table("users_log_table", sql_1) self.__helper.insert(sql, params) return True else: sql_2 = "select count(*) as count from users_log_table where username = '******'" % (username) data_dict = self.__helper.fetch_one(sql_2) if data_dict["count"]: return False # 该用户名已经存在 else: self.__helper.insert(sql, params) return True
def __init__(self): self.__helper = MySQLHelper()
def insert_mesg(uid,speaker,chat_text): #插入聊天信息到数据库 sql = 'insert into chat_records(uid,speaker,chat_text) values(%s,%s,%s);' params = (uid,speaker,chat_text) sql_helper = MySQLHelper() sql_helper.insert(sql, params) #插入数据
def select_mesg(uid): #查询指定用户的聊天信息 sql = 'select * from chat_records where uid=%s;' params = (uid,) sql_helper = MySQLHelper() return sql_helper.get_all(sql, params) #查询数据
def __init__(self, recv_data): self.__helper = MySQLHelper() self.recv_data = recv_data
def __init__(self,uid): self.uid = uid self.sqlhelper = MySQLHelper() self.gname = '' self.gnew_name = ''