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()
Setting advance
will add to the completed value by the specified ammount while setting completed
itself will override it.
Since data_sent
and data_received
are already sums, instead of
progress.update(task, advance=amount)
You probably want
progress.update(task, completed=amount)
Altenatively, you could also avoid keeping track of the total entirely, and just use the delta:
(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:
while True:
data = f.read(102400)
if not data:
break
delta = sock.send(data)
progress.update(send_task, advance=delta)
sock.close()