def upload(obj, header): path = get_path(DB_PATH, header['now_dir']) file_path = os.path.join(path, header['filename']) data_size = header['size'] dd_size = 0 with open(file_path, 'wb') as f: while dd_size < data_size: line = obj.request.recv(1024) dd_size += len(line) f.write(line) f.flush() header_dict = {'action': 'upload'} md5_x = common.file_md5(file_path) if md5_x == header['md5']: re_data = { 'code': 200, 'status': True, 'msg': '上传成功', } else: re_data = { 'code': 500, 'status': True, 'msg': '文件不一致', } obj_deal = my_agreement.MyAgreement(header_dict, re_data) re_res = obj_deal.deal_data() obj.request.send(re_res)
def check_file(obj, header): if header['now_dir']: path = get_path(DB_PATH, header['now_dir']) header_dict = {'action': 'check_file', 'size': 0} if os.path.exists(path): dir_list = os.listdir(path) re_data = { 'code': 200, 'status': True, 'msg': '当前目录文件如下:', 'data': dir_list } else: re_data = { 'code': 500, 'status': False, 'msg': '路径不存在', 'data': [] } else: re_data = {'code': 500, 'status': False, 'msg': '路径不存在', 'data': []} obj_deal = my_agreement.MyAgreement(header_dict, re_data) re_res = obj_deal.deal_data() obj.request.send(re_res)
def login(obj, header): data_size = header['size'] data_json = obj.request.recv(data_size).decode('utf-8') data = json.loads(data_json) user_info = common.query_file_pickle(DB_USER) if not user_info: user_info = {} header = {'action': 'login'} if data['name'] in user_info and user_info[ data['name']]['pwd'] == data['pwd']: mydir = os.listdir(user_info[data['name']]['mydir'][0]) re_data = { 'code': 200, 'status': True, 'msg': '登录成功', 'mydir': mydir, 'now_dir': [data['name']], 'user': user_info[data['name']], } else: re_data = {'code': 500, 'status': False, 'msg': '用户名或密码错误'} obj_deal = my_agreement.MyAgreement(header, re_data) re_res = obj_deal.deal_data() obj.request.send(re_res)
def register(name,pwd,client): m = hashlib.md5() m.update(pwd.encode('utf-8')) pwd = m.hexdigest() header_dict={ 'action': 'register', } data_dict={ 'name':name, 'pwd':pwd } obj=my_agreement.MyAgreement(header_dict,data_dict) data=obj.deal_data() client.send(data) res1=client.recv(4) header_size=obj.get_header_size(res1)[0] res2=client.recv(header_size) header=obj.get_header(res2) if header['action']=='register': data_size=header['size'] res=client.recv(data_size) res_all=obj.get_header(res) return res_all['status'],res_all['msg'] return False,'注册有误'
def change_dir(obj, header): if header['now_dir']: path = get_path(DB_PATH, header['now_dir']) file_path = os.path.join(path, header['path_name']) header_dict = {'action': 'change_dir', 'size': 0} if os.path.isdir(file_path): header['now_dir'].append(header['path_name']) re_data = { 'code': 200, 'status': True, 'msg': '切换成功', 'data': header['now_dir'] } else: re_data = { 'code': 500, 'status': False, 'msg': '这不是一个有效目录', 'data': header['now_dir'] } else: re_data = { 'code': 500, 'status': False, 'msg': '这不是一个有效目录', 'data': header['now_dir'] } obj_deal = my_agreement.MyAgreement(header_dict, re_data) re_res = obj_deal.deal_data() obj.request.send(re_res)
def register(obj,header): data_size=header['size'] data_json=obj.request.recv(data_size).decode('utf-8') data=json.loads(data_json) user_info=common.query_file_pickle(DB_USER) if not user_info:user_info={} header = { 'action': 'register' } if data['name'] not in user_info: path=os.path.join(DB_PATH,data['name']) os.mkdir(path) user_info[data['name']]={ 'pwd':data['pwd'], 'mydir':[path], 'mydir_size':0, 'max_size':102400000 } common.save_file_pickle(DB_USER,user_info) re_data = { 'code': 200, 'status': True, 'msg': '注册成功' } else: re_data={ 'code':500, 'status':False, 'msg':'用户名已存在' } obj_deal=my_agreement.MyAgreement(header,re_data) re_res=obj_deal.deal_data() obj.request.send(re_res)
def mk_dir(obj, header): path = get_path(DB_PATH, header['now_dir']) file_path = os.path.join(path, header['dirname']) header_dict = {'action': 'mk_dir', 'size': 0} if os.path.exists(file_path): re_data = {'code': 500, 'status': False, 'msg': '目录已存在'} else: re_data = {'code': 200, 'status': True, 'msg': '创建成功'} os.mkdir(file_path) obj_deal = my_agreement.MyAgreement(header_dict, re_data) re_res = obj_deal.deal_data() obj.request.send(re_res)
def register(name, pwd, client): header_dict = { 'action': 'register', } data_dict = {'name': name, 'pwd': pwd} obj = my_agreement.MyAgreement(header_dict, data_dict) data = obj.deal_data() client.send(data) res1 = client.recv(4) header_size = obj.get_header_size(res1)[0] res2 = client.client.recv(header_size) header = obj.get_header(res2) if header['action'] == 'register': data_size = header['size'] res = client.recv(data_size) res_all = obj.get_header(res) print(res_all)