def POST(self): web.header("Access-Control-Allow-Origin", "*") data = web.data() data = json.loads(data) print(data) action = data['action'] ids = data['ids'] if action == None or ids == None: return self.JSON({'code': '1:lack of data'}) elif ids == -1: con, cur = connect_db() cur.execute("update EVENT set state=?", (action, )) con.commit() save_log('INFO', 'all data state change to' + action) close_db(con, cur) elif ids != []: con, cur = connect_db() for id in ids: cur.execute("update EVENT set state=? where id=?", ( action, id, )) con.commit() save_log( 'DEBUG', 'data state change to {} where id is{}'.format(action, id)) close_db(con, cur) return self.JSON({'code': '0'})
def find_earliest(): timelist = [] con, cur = connect_db() cur.execute("SELECT happen_time FROM event") rawtimelist = cur.fetchall() close_db(con, cur) for i in rawtimelist: timelist.append(i[0]) if len(timelist) >= 2: timelist.sort() return timelist[0] else: return timelist[0]
def GET(self): get_dict = web.input() userName = get_dict.userName password = get_dict.get('password', None) password = base64.b64decode(password) con, cur = connect_db() cur.execute("select token from token where username='******'") token = cur.fetchall()[0][0] #数据库中对应的token cur.execute("select password from token where username='******'") pwd = cur.fetchall()[0][0] #数据库中对应的密码 close_db(con, cur) if userName == 'wnagnn': if password == pwd: result = {"status": 200, "token": token} else: result = {"status": 400, "errMsg": "password is error"} else: result = {"status": 400, "errMsg": "userName is error"} return self.JSON(result)
def GET(self): web.header("Access-Control-Allow-Origin", "*") get_dict = web.input() token = get_dict.get('token', None) start_time = get_dict.get('start_time', '0') end_time = get_dict.get( 'end_time', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) con, cur = connect_db() cur.execute('select token from token') real_token = cur.fetchall()[0][0] if token == real_token: save_log('INFO', 'token success, prepare for data') push_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') ids = get_weakness_ids() con, cur = connect_db() value = [] for i in ids: data = sql_select(i[0]) happen_time = data[7] if check_time(start_time, end_time, happen_time): data = transformData(i[0]) value.append(data) cur.execute("update EVENT set state=? where id=?", ( 'push', int(i[0]), )) cur.execute("update EVENT set push_time=? where id=?", ( push_time, int(i[0]), )) con.commit() close_db(con, cur) save_log('DEBUG', 'weakness database changed') save_log('INFO', 'generate weakness data successful') response = {"status": 200, "errMsg": 'success', "data": value} save_log('INFO', 'send weakness data...') return self.JSON(response) else: save_log('INFO', 'failed with invalide token') web.header("status_code", "400") response = {"status": 400, "errMsg": 'token is invalid'} return self.JSON(response)
def POST(self): web.header("Access-Control-Allow-Origin", "*") rawdata = web.data() rawdata = json.loads(rawdata) state = rawdata.get('state', None) action = rawdata.get('action', None) if action == None or state == None: return self.JSON({'code': '1:lack of data'}) else: con, cur = connect_db() cur.execute("update EVENT set state=? where state=?", ( action, state, )) con.commit() save_log( 'DEBUG', 'all data where state is {} change to {}'.format( state, action)) close_db(con, cur) return self.JSON({'code': '0'})
def get_weakness_ids(): con, cur = connect_db() cur.execute("SELECT ID FROM EVENT WHERE state='exist' AND type='weakness'") ids = cur.fetchall() close_db(con, cur) return ids