def sort_log(log): log_dic = {} log_list = open(log, 'rb') for line in log_list.readlines(): ip, url, code = line.split()[0], line.split()[6], line.split()[8] key = (ip, url, code) log_dic[key] = log_dic.get(key, 0) + 1 for lin in sorted(log_dic.items(), key=lambda x: -x[1])[:1000]: sql = 'insert into log (ip,url,code,count) values(%s,%s,%s,%s)' args = (lin[0][0], lin[0][1], lin[0][2], lin[1]) useruntils.execute_sql(sql, args, fetch=False)
def validate_login(username, password): #sql = 'select * from user where name="{username}" and password = "******"'.format(username=username,password=password) sql = 'select * from user where name=%s and password = md5(%s)' args = (username, password) count, rt_list = useruntils.execute_sql(sql, args, fetch=True) #return count != 0 if count: return True else: return False
def get_users(): _coulmns = ('id', 'username', 'password', 'age') sql = 'select * from user' args = () _rt = [] count, rt_list = useruntils.execute_sql(sql, args, fetch=True) if not count: return [] for lines in rt_list: _rt.append(dict(zip(_coulmns, lines))) return _rt
def select_log(id_c): sql = 'select * from log where id <= %s' args = (id_c) _count, _rt_list = useruntils.execute_sql(sql, args, fetch=True) print _rt_list _coulmns = ('id', 'ip', 'url', 'code', 'count') _rt = [] if not _count: return [] for lines in _rt_list: _rt.append(dict(zip(_coulmns, lines))) return _rt
def sort_user(order): sql = 'select * from user order by id {order}'.format(order=order) #sql = 'select * from user order by id %s' coulmns = ('id', 'username', 'password', 'age') args = () _rt = [] count, rt_list = useruntils.execute_sql(sql, args, fetch=True) if not count: return [] for lines in rt_list: _rt.append(dict(zip(coulmns, lines))) return _rt
def get_user(id): coulmns = ('id', 'username', 'password', 'age') sql = 'select * from user where id=%s' args = (id) count, rt_list = useruntils.execute_sql(sql, args, fetch=True) if not rt_list: return None else: _rt_list = rt_list[0] rt = dict(zip(coulmns, _rt_list)) if count: return rt else: return None
def get_username(username): coulmns = ('id', 'username', 'password', 'age') sql = 'select * from user where name="{username}"'.format( username=username) args = () count, rt_list = useruntils.execute_sql(sql, args, fetch=True) print rt_list if not rt_list: return None else: _rt_list = rt_list[0] rt = dict(zip(coulmns, _rt_list)) if count: return rt else: return None
def delete_user(id): sql = "delete from user where id=%s" args = id useruntils.execute_sql(sql, args, fetch=False)
def add_user(username, password, age): sql = 'insert into user(name,password,age) values(%s,md5(%s),%s)' args = (username, password, age) useruntils.execute_sql(sql, args, fetch=False)
def update_user(username, password, age, id): sql = 'update user SET name=%s,password=md5(%s),age=%s where id=%s' args = (username, password, age, id) useruntils.execute_sql(sql, args, fetch=False)