diff --git a/main.py b/main.py index 8c06fb2..a0abe10 100644 --- a/main.py +++ b/main.py @@ -964,11 +964,26 @@ bind_port = {bport} break if line.strip(): self.append_send_output(f"[P2P] {line}") - - # 进程结束后检查退出码 + + # 进程结束后检查退出码并记录结束时间 return_code = self.send_p2p_process.wait() + end_time = time.time() + end_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(end_time)) + + # 计算耗时 + duration = end_time - self.send_start_time + duration_str = self.format_duration(duration) + if self.is_send_running: - self.append_send_output(f"[P2P] 文件发送完成,退出码: {return_code}\n") + if return_code == 0: + self.append_send_output(f"[时间] 文件发送完成: {end_time_str}\n") + self.append_send_output(f"[统计] 总耗时: {duration_str}\n") + self.append_send_output("✅ 文件发送成功完成!\n") + else: + self.append_send_output(f"[时间] 文件发送异常结束: {end_time_str}\n") + self.append_send_output(f"[统计] 运行时间: {duration_str}\n") + self.append_send_output(f"❌ 文件发送失败,退出码: {return_code}\n") + self.send_file_button.config(state='normal') except Exception as e: @@ -1023,7 +1038,25 @@ bind_port = {bport} if not file_path or not os.path.exists(file_path): self.append_send_output("[错误] 文件路径无效或文件不存在\n") return - + + + # 记录开始时间 + self.send_start_time = time.time() + start_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.send_start_time)) + self.append_send_output(f"[时间] 文件发送开始: {start_time_str}\n") + + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + startupinfo.wShowWindow = 0 + + file_path = self.file_path_var.get() + file_size = os.path.getsize(file_path) + file_name = os.path.basename(file_path) + + self.append_send_output(f"[文件] 名称: {file_name}\n") + self.append_send_output(f"[文件] 大小: {self.format_file_size(file_size)}\n") + + self.send_p2p_process = subprocess.Popen( [ '.\\python.exe', @@ -1348,26 +1381,73 @@ local_port = {bport} break if line.strip(): self.append_output(f"[P2P] {line}") - - # 进程结束后检查退出码 + + # 进程结束后检查退出码并记录结束时间 return_code = self.p2p_process.wait() + end_time = time.time() + end_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(end_time)) + + # 计算耗时 + duration = end_time - self.receive_start_time + duration_str = self.format_duration(duration) + if self.is_running: - self.append_output(f"[P2P] P2P接收器已停止,退出码: {return_code}\n") + if return_code == 0: + self.append_output(f"[时间] 文件接收完成: {end_time_str}\n") + self.append_output(f"[统计] 总耗时: {duration_str}\n") + self.append_output("✅ 文件接收成功完成!\n") + else: + self.append_output(f"[时间] 文件接收异常结束: {end_time_str}\n") + self.append_output(f"[统计] 运行时间: {duration_str}\n") + self.append_output(f"❌ 文件接收失败,退出码: {return_code}\n") except Exception as e: self.append_output(f"[P2P] 输出监控错误: {e}\n") + def format_file_size(self, size_bytes): + """格式化文件大小""" + if size_bytes == 0: + return "0B" + + size_names = ["B", "KB", "MB", "GB", "TB"] + i = 0 + size = size_bytes + + while size >= 1024 and i < len(size_names) - 1: + size /= 1024 + i += 1 + + return f"{size:.2f} {size_names[i]}" + + def format_duration(self, seconds): + """格式化时间间隔""" + if seconds < 60: + return f"{seconds:.1f}秒" + elif seconds < 3600: + minutes = seconds // 60 + seconds = seconds % 60 + return f"{int(minutes)}分{int(seconds)}秒" + else: + hours = seconds // 3600 + minutes = (seconds % 3600) // 60 + seconds = seconds % 60 + return f"{int(hours)}时{int(minutes)}分{int(seconds)}秒" + def execute_p2p_receiver(self): """执行P2P文件接收器""" try: self.current_stage = "p2p" self.update_status("正在启动P2P文件接收...") + # 记录开始时间 + self.receive_start_time = time.time() + start_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.receive_start_time)) + self.append_output(f"[时间] 文件接收开始: {start_time_str}\n") # 获取用户选择的保存路径 save_path = self.receive_path_var.get().strip() if not save_path: - save_path = ".\\received_files" # 默认路径 - + save_path = ".\\received_files" + # 创建目录(如果不存在) os.makedirs(save_path, exist_ok=True)