def login_out(sock, data): if data['ID'] in server.login_users: server.login_users.remove(data['ID']) print(server.login_users) d = json.dumps({"result": "login out succeed"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d) db = ConnDb() db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{data['ID']}','login_out','success')" )
def save_error(sock, opr): db = ConnDb() try: data = opr['data'] ID = generate_ID() table = 'error' cols = ('ID', 'pos', 'type', 'detail', 'OID') values = f"('{ID}','{data['pos']}','{data['type']}','{data['detail']}','{data['OID']}')" result = db.insert(table, cols, values) if result == 'success': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','save error','success','{ID}')" ) d = json.dumps({"result": "save error success"}).encode('utf-8') else: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','save error','fail')" ) d = json.dumps({"result": "save error failed"}).encode('utf-8') except: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','save error','fail')" ) d = json.dumps({"result": "save error failed"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d)
def delete_err(sock, opr): db = ConnDb() try: result = db.delete('error', f"ID = '{opr['ID']}'") if result == 'success': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','delete error','success','{opr['ID']}')" ) d = json.dumps({"result": "delete error success"}).encode('utf-8') else: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','delete error','fail','{opr['ID']}')" ) d = json.dumps({"result": "delete error fail"}).encode('utf-8') except: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','delete error','fail','{opr['ID']}')" ) d = json.dumps({"result": "delete error fail"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d)
def login(sock, data): db = ConnDb() result = db.select('name,password', 'user', "ID='" + str(data['ID']) + "'") if len(result) == 0: sock.send(b'4') print(sock.recv(1024).decode('utf-8')) sock.send(b'fail') db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{data['ID']}','login_in','fail')" ) elif result[0][1] == data['password']: server.login_users.append(data['ID']) print(server.login_users) d = json.dumps({ "result": "login in success", "name": result[0][0] }).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d) db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{data['ID']}','login_in','success')" ) else: d = json.dumps({"result": "fail"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d) db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), values= f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{data['ID']}','login_in','fail','none')" )
def upload(sock, opr): # 接收文件 file = b'' e = sock.recv(1024).decode('utf-8') num = int(e) sock.send(b'Confirm') e = sock.recv(1024) while len(file) < num - 1024: file += e e = sock.recv(1024) file += e print(opr) db = ConnDb() OID = generate_ID() # 插入实际文件 if opr['type'] == 'text': result = db.insert_file('txt', ('ID', 'data'), (f"'{OID}'", f"'{file.decode('utf-8')}'")) else: result = db.insert_file('image', ('ID', 'data'), (f"'{OID}'", file)) if result == 'success': # 插入索引文件 ID = generate_ID() table = 'map' cols = ('ID', 'title', 'type', 'OID') values = f"('{ID}','{opr['title']}','{opr['type']}','{OID}')" result = db.insert(table, cols, values) if result == 'success': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','upload','success','{ID}')" ) d = json.dumps({"result": "file upload success"}).encode('utf-8') else: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','upload','fail')" ) d = json.dumps({"result": "file upload failed"}).encode('utf-8') else: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','upload','fail')" ) d = json.dumps({"result": "file upload failed"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d)
def download(sock, opr): db = ConnDb() try: OID = db.select('OID', 'map', f"ID = '{opr['ID']}'")[0][0] if opr['type'] == 'image': file = db.select('data', 'image', f"ID = '{OID}'")[0][0] else: file = db.select('data', 'txt', f"ID = '{OID}'")[0][0].encode('utf-8') sock.send(str(len(file)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.sendall(file) if opr['operation'] == 'download file': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','download file','success','{opr['ID']}')" ) elif opr['operation'] == 'open project: download': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','open project','success','{opr['ID']}')" ) except: if opr['operation'] == 'download file': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','download file','failed','{opr['ID']}')" ) elif opr['operation'] == 'open project: download': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','open project','failed','{opr['ID']}')" )
def edit_exist_err(sock, opr): db = ConnDb() try: data = opr['data'] values = (f"'{data['pos']}'", f"'{data['type']}'", f"'{data['detail']}'") result = db.update('error', ('pos', 'type', 'detail'), values, f"ID = '{data['ID']}'") if result == 'success': # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','edit exist error','success','{opr['data']['ID']}')" ) d = json.dumps({ "result": "edit exist error success" }).encode('utf-8') else: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','edit exist error','fail','{opr['data']['ID']}')" ) d = json.dumps({"result": "edit exist error fail"}).encode('utf-8') except: # 插入操作记录 db.insert( 'operation', ('opr_time', 'user', 'operation', 'result', 'object'), f"('{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}','{opr['user']}','edit exist error','fail','{opr['data']['ID']}')" ) d = json.dumps({"result": "edit exist error fail"}).encode('utf-8') sock.send(str(len(d)).encode('utf-8')) print(sock.recv(1024).decode('utf-8')) sock.send(d)