Browse Source

增加开始和结束时间

master
zhanglei 3 months ago
parent
commit
977a8c108e
  1. 90
      main.py

90
main.py

@ -965,10 +965,25 @@ bind_port = {bport}
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:
@ -1024,6 +1039,24 @@ bind_port = {bport}
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',
@ -1349,24 +1382,71 @@ local_port = {bport}
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)

Loading…
Cancel
Save