[Python] rich progress bar not showing correct results with sockets file sharing

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have two scripts sender.py and reciever.py. i got the idea of calculating chunks recieved and chunks sent from here.

    but i got some different results. the progress bar completed 100% instantly as shown here. am i doing something wrong?

    sender.py

    import socket
    from pip._vendor.rich.progress import (Progress,TextColumn,BarColumn,TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

    sock = socket.socket()
    sock.connect(('127.0.0.1',9999))
    filename = "C:\\Users\\usename\\Downloads\\ubuntu-23.04-desktop-amd64.iso"
    filesize = 4932407296
    with Progress(TextColumn("[progress.description]{task.description}"), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn('at'), TransferSpeedColumn()) as progress:
    send_task = progress.add_task("Sending...", total=filesize)
    with open(filename, 'rb') as f:
    data_sent = 0
    while True:
    data = f.read(102400)
    if not data:
    break
    data_sent += sock.send(data)
    progress.update(send_task, advance=data_sent)
    sock.close()


    reviever.py

    import socket
    from pip._vendor.rich.progress import (Progress,TextColumn,BarColumn,TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

    sock = socket.socket()
    sock.bind(("127.0.0.1",9999))
    sock.listen(10) # Accepts up to 10 incoming connections..
    conn, address = sock.accept()
    filename = 'ubuntu-23.04-desktop-amd64.iso'
    filesize = 4932407296
    print( address)
    with Progress(TextColumn("[progress.description]{task.description}"), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn('at'), TransferSpeedColumn()) as progress:
    recv_task = progress.add_task("Recieving...", total=filesize)
    with open(filename,'wb') as f:
    data_recieved = 0
    while (data_recieved < filesize):
    data = conn.recv(102400)
    data_recieved += f.write(data)
    progress.update(recv_task, advance=data_recieved)
    conn.close()
    sock.close()

    Continue reading...

Compartilhe esta Página