Download-Fortschritt in PythonPython

Python-Programme
Guest
 Download-Fortschritt in Python

Post by Guest »

Ich habe ein Client-Server-Programm geschrieben, der Client lädt Dateien in Blöcken vom Server herunter und ich möchte den Download-Fortschritt auf der Client-Konsole anzeigen.
Ich habe diesen Code ausprobiert:

Code: Select all

def download_chunk(index, offset, size):
"""Download a file chunk."""
try:
if size == 0:
with progress_lock:
progress[index] = 100.00
return

with open(temp_files[index], "wb") as f:
connections[index].send(f"DOWNLOAD {filename} {offset} {size}".encode("utf8"))
remaining = size
total_received = 0
while remaining > 0:
chunk = connections[index].recv(min(BUFSIZ, remaining))
if not chunk:
raise ConnectionError(f"Connection lost for chunk {index}.")
f.write(chunk)
received_len = len(chunk)
total_received += received_len
remaining -= received_len
with progress_lock:
progress[index] = (total_received / size) * 100
if progress_lines[index] is None:
print(f"Downloading {filename} part {index + 1} .... {progress[index]:6.2f}%")
progress_lines[index] = index
else:
print(f"\033[FDownloading {filename} part {index + 1} ...   {progress[index]:6.2f}%")
except Exception as e:
print(f"Error downloading chunk {index}: {e}")
finally:
connections[index].close()
Ich gehe davon aus, dass die Client-Konsole so aussehen würde

Code: Select all

Downloading File5.zip part 1 ....  100.00%
Downloading File5.zip part 2 ....  100.00%
Downloading File5.zip part 3 ....  100.00%
Downloading File5.zip part 4 ....  100.00%
Aber nach dem Download sieht es so aus

Code: Select all

Downloading Pickleball.mp3 part 1 ....  29.02%
Downloading Pickleball.mp3 part 2 ....  21.73%
Downloading Pickleball.mp3 part 2 ....  45.86%
Downloading Pickleball.mp3 part 4 .... 100.00%
Die Ausgabe
geben Sie hier die Bildbeschreibung ein
Es hat die Zeile [FDownloading Pickleball.mp3 Teil 1 .... 100,00% aber nach dem Herunterladen betrug 29,02 %
Ich weiß nicht, wo ich falsch liege

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post