def send_filedata(self): readed_size = 0 with open(self.filename, 'rb') as f: filedata = f.read(4096) while len(filedata) > 0: tempsize = len(filedata) readed_size += tempsize self.mission_read_size += tempsize try: self.socket.send(filedata) readed_show = '%s/%s' % (formated_size(readed_size), formated_size(self.filesize)) total_readed_show = '%s/%s' % (formated_size( self.mission_read_size), formated_size(sumsize)) current_filename = os.path.basename(self.filename) + ' ' sys.stdout.write( current_filename + readed_show + ' | %.2f%% >>>%s %s | %.2f%%' % (float(readed_size / self.filesize * 100), dict('total'), total_readed_show, float(self.mission_read_size / sumsize * 100)) + '\r') except BrokenPipeError as e: if self.once: if self.filefinder: self.filefinder.finderCallback = None self.filefinder.recycle = True self.filefinder.off = True print(right_arrows + dict('rcd') + left_arrows) self.once = False filedata = f.read(4096) print() if readed_size == self.filesize and readed_size == 0: print(os.path.basename(self.filename) + ' %s' % dict('fi')) print('—' * 30) if self.singFile: self.commandThread.send_command(COMMAND_CLOSE) self.socket.close() self.commandThread.working = False print( dict('cmct') + '%s' % formated_time(time.time() - self.commandThread.start_time)) else: if self.findfileOver: self.commandThread.send_command(COMMAND_CLOSE) self.socket.close() self.commandThread.working = False print( dict('cmct') + '%s' % formated_time(time.time() - self.commandThread.start_time))
def run(self): self.ssocket = socket.socket() self.ssocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: self.ssocket.bind((self.host, self.port)) self.ssocket.listen(1) while self.wait_client_flag: socket2, _ = self.ssocket.accept() self.socket = socket2 self.socket.send( bytes('连接到指令套接字:' + self.host + ':' + str(self.port), encoding='utf8')) self.commandMessenger = Messenger(self.socket) command = self.commandMessenger.recv_msg() self.start_time = time.time() while command and len(command) and self.working > 0: if command.startswith(COMMANE_MISSION_SIZE): self.mission_size = int(command.split(divider_arg)[1]) print('任务大小' + ': %s' % formated_size(self.mission_size)) elif command.startswith(COMMANE_FILE_INFO): self.fileMission = FileMission( self.dataThread.socket, self, self.dataThread.save_path, command) self.fileMission.start() self.dataOn = True elif command == COMMAND_CLOSE: self.dataOn = False time.sleep(0.3) Warning(right_arrows + '远端连接断开' + left_arrows) command = self.commandMessenger.recv_msg() except OSError: warning('无法绑定到目标地址') self.wait_client_flag = False
def onFindFile(self,file_path,size): global msg_index msg_index += 1 print('文件 :'+ os.path.basename(self.rootpath)+dir_divider()+relative_path(self.rootpath,file_path) + ' '+ formated_size(size)) self.filename = file_path self.filesize = size if (os.path.isfile(file_path) and relative_path(self.rootpath,file_path) == ''): self.commandThread.send_fileinfo(COMMANE_FILE_INFO + divider_arg + os.path.basename(file_path) + divider_arg+ str(size) + divider_arg+ getFileMd5(file_path) + divider_arg+ str(msg_index)) else: self.commandThread.send_fileinfo(COMMANE_FILE_INFO + divider_arg + os.path.basename(self.rootpath) + dir_divider()+ relative_path(self.rootpath,file_path) + divider_arg+ str(size) + divider_arg + getFileMd5(file_path) + divider_arg + str(msg_index))
host_ok = True if port_ok and filepath_ok and host_ok: findercallback = MyfinderCallback() if not checkfile(filepath)[1]: print('%s %s' % ('在此查找文件:',filepath)) FileFinder_Fast(findercallback).list_flie(filepath) global sumsize sumsize = findercallback.sumsize file_type = '' if checkfile(filepath)[1]: file_type = '文件 :' else: file_type = '文件夹(包括在内的所有文件):' warning('%s %s' % ('您准备发送的是',file_type)) print(filepath + ' | %s%s : %s' % ('总计','大小',formated_size(sumsize))) confirm = input('%s(Y/N):'% '继续发送? ') while True: if confirm == 'y' or confirm == 'Y' or confirm == 'Yes'.upper() or confirm == 'yes'.lower(): commandThread = None client = Client(host, port) commandThread = CommandThread(host) commandThread.setDataThread(client) commandThread.setMissionSize(sumsize) client.setCommandThread(commandThread) client.setFilePath(filepath) client.start() commandThread.start() break elif confirm == 'n' or confirm == 'N' or confirm == 'No'.upper() or confirm == 'no'.lower():
if port_ok and filepath_ok and host_ok: findercallback = MyfinderCallback() if not checkfile(filepath)[1]: print('%s %s' % (dict('ffi'), filepath)) FileFinder_Fast(findercallback).list_flie(filepath) global sumsize sumsize = findercallback.sumsize file_type = '' if checkfile(filepath)[1]: file_type = dict('tf') else: file_type = dict('td') warning('%s %s' % (dict('ya'), file_type)) print(filepath + ' | %s%s : %s' % (dict('total'), dict('si'), formated_size(sumsize))) confirm = input('%s(Y/N):' % dict('ct')) while True: if confirm == 'y' or confirm == 'Y' or confirm == 'Yes'.upper( ) or confirm == 'yes'.lower(): commandThread = None client = Client(host, port) commandThread = CommandThread(host) commandThread.setDataThread(client) commandThread.setMissionSize(sumsize) client.setCommandThread(commandThread) client.setFilePath(filepath) client.start() commandThread.start() break
def write_filedata(self): print( dict('st') + '%s %s' % (self.filename, formated_size(self.filesize))) if getFileMd5(self.file_path) == self.file_md5: print(dict('fe') + self.filename) self.commandThread.wrote_size += self.filesize self.commandThread.file_existed(self.fileinfo) downloaded_show = '%s/%s' % (formated_size( self.filesize), formated_size(self.filesize)) total_downloaded_show = '%s/%s' % ( formated_size(self.commandThread.wrote_size), formated_size(self.commandThread.mission_size)) current_filename = os.path.basename(self.filename) + ' ' print(current_filename + downloaded_show + ' | %.2f%% >>>%s %s | %.2f%%' % (float(self.filesize / self.filesize * 100), dict('total'), total_downloaded_show, float(self.commandThread.wrote_size / self.commandThread.mission_size * 100)) + '\r') print('-' * 30) if self.commandThread.wrote_size == self.commandThread.mission_size and self.commandThread.wrote_size != 0: self.commandThread.wrote_size = 0 self.commandThread.mission_size = 0 print(right_arrows + dict('mc') + left_arrows) print( dict('cmct') + '%s' % formated_time(time.time() - self.commandThread.start_time)) print( dict('ra') + ' (%s,%d)' % (self.commandThread.dataThread.host, self.commandThread.dataThread.port)) print(dict('wfft') + '...') return if self.filesize == 0: with open(self.file_path, 'wb') as f: pass self.commandThread.file_existed(self.fileinfo) return self.commandThread.file_ready(self.fileinfo) with open(self.file_path, 'wb') as f: wrote_size = 0 filedata = self.socket.recv(4096) while len(filedata) > 0: tempsize = f.write(filedata) wrote_size += tempsize self.commandThread.wrote_size += tempsize f.flush() downloaded_show = '%s/%s' % (formated_size(wrote_size), formated_size(self.filesize)) total_downloaded_show = '%s/%s' % ( formated_size(self.commandThread.wrote_size), formated_size(self.commandThread.mission_size)) current_filename = os.path.basename(self.filename) + ' ' sys.stdout.write( current_filename + downloaded_show + ' | %.2f%% >>>%s %s | %.2f%%' % (float(wrote_size / self.filesize * 100), dict('total'), total_downloaded_show, float(self.commandThread.wrote_size / self.commandThread.mission_size * 100)) + '\r') if wrote_size == self.filesize: print() print(self.filename + ' ' + dict('dd')) self.commandThread.file_transportover(self.fileinfo) if not self.commandThread.dataOn: self.socket.close() break else: try: filedata = self.socket.recv(4096) except ConnectionResetError: warning(right_arrows + dict('rcd') + left_arrows) if wrote_size < self.filesize: warning(right_arrows + dict('ci') + left_arrows) self.dataOn = False self.socket.close() self.commandThread.socket.close() self.commandThread.wrote_size = 0 self.commandThread.mission_size = 0 print('-' * 30) if self.commandThread.wrote_size == self.commandThread.mission_size and self.commandThread.wrote_size != 0: self.commandThread.wrote_size = 0 self.commandThread.mission_size = 0 print(right_arrows + dict('mc') + left_arrows) print( dict('cmct') + '%s' % formated_time(time.time() - self.commandThread.start_time)) print( dict('ra') + ' (%s,%d)' % (self.commandThread.dataThread.host, self.commandThread.dataThread.port)) print(dict('wfft') + '...')