def register(self, args): name = args['name'] passwd = common.my_md5('20180610'+name,args['passwd']) person_info_dic = self.data_handle.select_data('person_info') info_dic = {} if name in person_info_dic.keys(): info_dic['state'] = 0 print('该用户已经存在') else: info_dic['state'] = 1 # 实例化一个用户 person = modes.Member('1', name, passwd, args['age'], args['sex'], args['phone']) person_info_dic[name] = person.get_info() # 创建用户对应文件目录 flag = common.create_dir(name) if flag: info_dic['info'] = {'create_dir': 1} # 保存已经注册的用户的数据 self.data_handle.save_data(person_info_dic, 'person_info') print('注册成功!') else: info_dic['info'] = {'create_dir': 0} print('注册失败!') head_json_bytes, head_struct = common.struct_pack(info_dic) common.send_info(head_json_bytes, head_struct, self.request)
def download_files(self, args): # 接收文件上传,获取上传过来的文件名,将文件接收保存到本地 file_path = os.path.normpath(os.path.join( self.FILE_PATH, args['current_file'], args['filename'] )) name = args['name'] is_br_file = args['is_br_file'] head_dic = {'name': name} # 判断该文件是否是断点续传的文件 br_file_path = os.path.join(args['current_file'], args['filename']) data = common.is_br_file('download', br_file_path, name) br_flag = 0 if len(data) and is_br_file: # 是一致的 下一不判断MD5是否一致 if data['br_file_md5'] == args['br_file_md5']: head_dic['state'] = 1 # 发送一个信息包,之后再发送数据 seek = data['seek'] else: # MD5不一致 head_dic['state'] = 3 seek = 0 br_flag = 1 elif len(data) or is_br_file: seek = 0 head_dic['state'] = 2 head_dic['br_download_info'] = data else: # 全新上传的文件 head_dic['state'] = 1 seek = 0 head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request) head_dic = common.recv_info(self.request, self.coding) # print("======is_recv========",head_dic) # 断点之前的数据是可以的 file_path, recv_size, filesize, max_packet_size, file_seek, socket if head_dic['is_recv']: # 继续下载文件 filesize = os.path.getsize(file_path) head_dic = {'name':args['name'],'current_file':args['current_file'],'filename':args['filename'], 'filesize':filesize,'seek':seek} head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request) # file_path, filesize, file_seek, socket, size=1024 common.send_file_info(file_path, filesize, seek, self.request, self.max_packet_size)
def secure_connect(self, salt): head_dic = common.recv_info(self.sk, self.coding)['clear'] # 加密 Secure connection verification scv = common.my_md5(salt, head_dic) head_dic = {'scv': scv} head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding)['state'] return head_dic
def request_and_response(self, args): head_json_bytes, head_struct = common.struct_pack(args) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) # 登录之后获取 断点续传文件,避免每次上传下载 去 读取上传下载文件,减少对os的访问 if args['cmd'] == 'login' and head_dic['state'] == 1: self.br_download_info = head_dic['br_download_info'] self.br_upload_info = head_dic['br_upload_info'] return head_dic
def secure_connect(self): salt = settings.configuration.get('salt') random_clear = str(os.urandom(32)) # 封装明文 head_dic = {'clear': random_clear} head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request) # 接收校验密文,自加密 scv = common.my_md5(salt, random_clear) head_json = common.recv_info(self.request, self.coding)['scv'] if head_json == scv: head_dic = {'state': True} print("安全连接!") else: head_dic = {'state': False} print("非安全连接!") head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request) return head_dic
def login(self, args): name = args['name'] passwd = common.my_md5('20180610'+name, args['passwd']) person_info_dic = self.data_handle.select_data('person_info') person_login_info = self.data_handle.select_data('person_login_info') person_login_info = {} # print("person_info_dic = ", person_info_dic) info_dic = {} if name in person_login_info.keys(): info_dic['state'] = 3 print("该用户已经登录!") else: if name in person_info_dic.keys(): if person_info_dic[name]['passwd'] == passwd: # 登录人员的文件目录 self.USER_DIR = name info_dic['state'] = 1 info_dic['name'] = name person_info_dic[name].pop('passwd') # 去除密码 info_dic['person_info'] = person_info_dic[name] # 获取该用户当前目录下的文件信息 file_path = os.path.join(self.FILE_PATH, name) info_dic['file_list'] = os.listdir(file_path) info_dic['current_file'] = name # 获取断点续传文件 br_download_info = common.get_br_file('download', name) br_upload_info = common.get_br_file('upload', name) print('br_download_info br_upload_info = ', br_download_info, br_upload_info) info_dic['br_download_info'] = br_download_info info_dic['br_upload_info'] = br_upload_info print('br_download_info br_upload_info = ', info_dic['br_download_info'], info_dic['br_upload_info']) # 保存登录信息 login_info = {name: 1} self.data_handle.save_data(login_info, 'person_login_info') self.USERS_LIST = os.listdir(os.path.join(self.FILE_PATH, name)) print("登录成功!") else: info_dic['state'] = 0 print("用户名或密码错误!") else: info_dic['state'] = 2 print('该用户名不存在!') head_json_bytes, head_struct = common.struct_pack(info_dic) common.send_info(head_json_bytes, head_struct, self.request)
def file_operate(self, args): # cmd 文件路径 放回该路径下的文件信息 dir cd info {'cmd':'listdir','file_path':'xxx'} # info {'cmd':'mkdir','file_path':'xxx'} cmd = args['cmd'] current_file = args['current_file'] name = args['name'] file_name = args['file_path'] head_dic = {'name': name} print("cmd,file_name, current_file = ", cmd, file_name, current_file) if cmd == 'mkdir': try: os.mkdir(os.path.join(self.FILE_PATH, current_file, file_name)) head_dic['info'] = '\033[1;31;0m文件创建成功!\033[0m' except Exception as e: print(e) head_dic['info'] = '\033[1;31;0m文件创建失败!\033[0m' elif cmd == 'cd': # 判断文件目录是否存在,并且判断是否越界 file_path = os.path.join(self.FILE_PATH, file_name) if (file_name not in self.USERS_LIST) and os.path.exists(file_path) and os.path.isdir(file_path): current_file = file_name head_dic['info'] = '\033[1;31;0m文件目录切换成功!\033[0m' else: head_dic['info'] = '\033[1;31;0m %s:不合理,操作失败!\033[0m' % file_name elif cmd == 'rename': old_file_name = os.path.join(self.FILE_PATH, current_file, args['old_file_name']) new_file_name = os.path.join(self.FILE_PATH, current_file, args['new_file_name']) try: os.rename(old_file_name, new_file_name) head_dic['info'] = '\033[1;31;0m文件修改成功!\033[0m' except Exception as e: head_dic['info'] = '\033[1;31;0m文件修改失败!\033[0m' else: head_dic['info'] = '\033[1;31;0m操作成功!\033[0m' file_list = common.get_files(os.path.join(self.FILE_PATH, current_file)) head_dic['file_list'] = file_list head_dic['current_file'] = current_file print("file_list = ", file_list) head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request)
def handle(self): print(self.request) if self.secure_connect()['state']: while True: head_dic = common.recv_info(self.request, self.coding) cmd = head_dic['cmd'] if hasattr(self, cmd): func = getattr(self, cmd) func(head_dic['info']) else: info_dic = {'state': 9} head_json_bytes, head_struct = common.struct_pack(info_dic) common.send_info(head_json_bytes, head_struct, self.request) print('%s is not exists!' % cmd) return else: pass
def upload_files(self, args): # 接收文件上传,获取上传过来的文件名,将文件接收保存到本地 file_path = os.path.normpath(os.path.join( self.FILE_PATH, args['current_file'], args['filename'] )) name = args['name'] is_br_file = args['is_br_file'] head_dic = {'name': name} # 判断该文件是否是断点续传的文件 br_file_path = os.path.join(args['current_file'], args['filename']) data = common.is_br_file('upload', br_file_path, name) br_flag = 0 if len(data) and is_br_file: # 是一致的 下一不判断MD5是否一致 # br_file_md5(file_path, file_seek, size=1024) print("data['seek'] ==",data['seek']) print("br_file_path === ", br_file_path) print("file_path === ", file_path) br_file_md5 = str(common.br_file_md5(file_path,data['seek'],self.max_packet_size)) print('data[br_file_md5] == args[br_file_md5] = ', br_file_md5, args['br_file_md5']) if br_file_md5 == args['br_file_md5']: head_dic['state'] = 1 # 发送一个信息包,之后再发送数据 seek = data['seek'] else: # MD5不一致 head_dic['state'] = 3 seek = 0 br_flag = 1 elif len(data) or is_br_file: seek = 0 head_dic['state'] = 2 head_dic['br_upload_info'] = data else: # 全新上传的文件 head_dic['state'] = 1 seek = 0 head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request) head_dic = common.recv_info(self.request, self.coding)['info'] if head_dic['is_recv']: # 断点之前的数据是可以的 # file_path, filesize, file_seek, socket, size=1024 filesize = head_dic['filesize'] file_md5 = head_dic['file_md5'] if seek == 0: mode = 'wb' else: mode = 'ab' recv_seek, rev_file_md5 = common.recv_file_info(file_path, filesize, seek, self.request, self.max_packet_size,mode) if br_flag: rev_file_md5 = str(common.file_md5(file_path)) else: rev_file_md5 = str(rev_file_md5) print("rev_file_md5 == file_md5 =", rev_file_md5, file_md5) if rev_file_md5 == file_md5: head_dic = {'state': True, 'recv_size': recv_seek, 'name': name} print('upload success!') if br_flag: data = common.get_br_file('upload', name) print("data === ",data) data.pop(br_file_path) common.update_br_file(name, 'upload', data) else: head_dic = {'state': False, 'recv_size': recv_seek, 'name': name} # 保存没有成功上传的文件 type, file_path, time, num, seek, size, name data = common.is_br_file('upload', br_file_path, name) num = 0 if len(data): num = data['num'] + 1 # type, file_path, br_file_md5, time, num, seek, size, name common.save_br_file('upload', br_file_path, rev_file_md5, time.strftime(self.TIME_FORMAT), num, recv_seek, recv_seek, name) print('upload fail!') head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.request)
def upload_files(self, args): br_flag = 0 args_info = args['info'] name = args_info['name'] ret = {'state': True} # 本地化判断是不是断点续传文件 br_file_path = os.path.join(args_info['current_file'], args_info['filename']) if br_file_path in self.br_upload_info.keys(): br_file_info = self.br_upload_info[br_file_path] # 是断点续传的文件,发送服务器端确认 如果信息不一致更新客户端信息 br_file_md5 = str( common.br_file_md5(args_info['file_path'], br_file_info['seek'], self.max_packet_size)) head_dic = { 'cmd': 'upload_files', 'info': { 'name': name, 'is_br_file': True, 'current_file': args_info['current_file'], 'filename': args_info['filename'], 'br_file_md5': br_file_md5 } } head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) # 判断信息是否一致 state:1 完全一致,state:2 不是br文件 state:3 MD5不一致 state = head_dic['state'] if state == 1: # 发送数据 file_seek = br_file_info['seek'] br_flag = 1 elif state == 2: # 信息不一致,更新客户端数据,从新发送数据 # 信息不一致,更新客户端数据 self.br_upload_info = head_dic['br_upload_info'] common.update_br_info('upload', name, self.br_upload_info) file_seek = 0 if args_info['filename'] in args_info['file_list']: flag = input("该文件已经存在,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} elif state == 3: # MD5不一致,从新发送数据 # print("1234567890") flag = input("客户端和服务器端文件内容不一致,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} file_seek = 0 br_flag = 1 else: head_dic = { 'cmd': 'upload_files', 'info': { 'name': name, 'is_br_file': False, 'current_file': args_info['current_file'], 'filename': args_info['filename'], 'br_file_md5': '' } } head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) state = head_dic['state'] if state == 2: # 信息不一致,更新客户端数据,从新发送数据 # 信息不一致,更新客户端数据 self.br_upload_info = head_dic['br_upload_info'] common.update_br_info('upload', name, self.br_upload_info) br_file_md5 = str( common.br_file_md5(args_info['file_path'], head_dic['seek'])) if br_file_md5 == head_dic['br_file_md5']: file_seek = head_dic['seek'] br_flag = 1 else: file_seek = 0 if args_info['filename'] in args_info['file_list']: flag = input("该文件已经存在,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} if ret['state'] == True: head_json_bytes, head_struct = common.struct_pack(args) common.send_info(head_json_bytes, head_struct, self.sk) # file_path, filesize, file_seek, socket, size=1024 common.send_file_info(args_info['file_path'], args_info['filesize'], file_seek, self.sk, self.max_packet_size) recv_info = common.recv_info(self.sk, self.coding) if recv_info['state'] and br_flag: self.br_upload_info.pop(br_file_path) common.update_br_info('upload', recv_info['name'], self.br_upload_info) else: return recv_info else: head_dic = { 'cmd': 'download_files', 'info': { 'name': name, 'is_recv': False } } head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) return ret
def download_files(self, args): args_info = args['info'] # 本地化判断是不是断点续传文件 br_file_path = os.path.join(settings.FILE_PATH, args_info['current_file'], args_info['filename']) br_flag = 0 ret = {'state': True} if br_file_path in self.br_download_info.keys(): # 是断点续传的文件,发送服务器端确认 如果信息不一致更新客户端信息 br_file_info = self.br_download_info[br_file_path] br_file_md5 = str( common.br_file_md5(args_info['file_path'], br_file_info['seek'], 0)) head_dic = { 'cmd': 'download_files', 'info': { 'name': args_info['name'], 'is_br_file': True, 'current_file': args_info['current_file'], 'filename': args_info['filename'], 'br_file_md5': br_file_md5 } } head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) # 判断信息是否一致 state:1 完全一致,state:2 不是br文件 state:3 MD5不一致 state = head_dic['state'] if state == 1: # 接收数据 br_flag = 1 elif state == 2: # 信息不一致,更新客户端数据,从新发送数据 # 信息不一致,更新客户端数据 self.br_download_info = head_dic['br_download_info'] common.update_br_info('download', args_info['name'], self.br_download_info) if args_info['filename'] in args_info['file_list']: flag = input("该文件已经存在,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} elif state == 3: # MD5不一致,从新发送数据 flag = input("客户端和服务器端文件内容不一致,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} br_flag = 1 else: head_dic = { 'cmd': 'download_files', 'info': { 'name': args_info['name'], 'is_br_file': False, 'current_file': args_info['current_file'], 'filename': args_info['filename'], 'br_file_md5': '' } } head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) state = head_dic['state'] if state == 2: # 信息不一致,更新客户端数据,从新发送数据 # 信息不一致,更新客户端数据 self.br_download_info = head_dic['br_download_info'] common.update_br_info('download_files', args_info['name'], self.br_download_info) br_file_md5 = str( common.br_file_md5(args_info['file_path'], head_dic['file_seek'])) if br_file_md5 == head_dic['br_file_md5']: br_flag = 1 else: if args_info['filename'] in args_info['file_list']: flag = input("该文件已经存在,是否继续(Y/N):").strip() if flag.upper() != 'Y': ret = {'state': 'N'} if ret['state'] == True: head_dic = {'name': args_info['name'], 'is_recv': True} head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) head_dic = common.recv_info(self.sk, self.coding) file_path = os.path.join(settings.FILE_PATH, head_dic['filename']) # file_path, filesize, file_seek, socket, size=1024 if head_dic['seek'] == 0: mode = 'wb' else: mode = 'ab' recv_seek, rev_file_md5 = common.recv_file_info( file_path, head_dic['filesize'], head_dic['seek'], self.sk, self.max_packet_size, mode) name = head_dic['name'] file_mad5 = str(common.file_md5(file_path)) if rev_file_md5 == file_mad5: print('download success!') else: print('download fail!') else: head_dic = {'name': args['name'], 'is_recv': False} head_json_bytes, head_struct = common.struct_pack(head_dic) common.send_info(head_json_bytes, head_struct, self.sk) return ret