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 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 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 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 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