On 14/09/06, Diez B. Roggisch <de***@nospam.web.dewrote:
billie schrieb:
Hi all. I'm writing a TCP-based application that I will use to trasfer
binary files through the network. This piece of code represents how do
I get a file from a remote peer and save it on my local hard drive:
file_obj = open('downloaded.ext', 'wb')
while 1:
buf = sock.recv(2048)
if len(buf) == 0:
break
file_obj.write(buf)
file_obj.close()
sock.close()
I would like to know how could be possible to limit the file transfer
speed (for example: don't write more than 50 Kb/sec).
Some ideas?
If you are on unix, use trickle. If you must limit it from within your
own code, I can only assume that computing the transfer rate so far and
introducing timeouts might help - but I never did such a thing, nor do I
know what that means for example for the network stack.
But maybe even then trickle may help you to get an idea, as it is a
user-space program AFAIK. So they might have some information (at least
the code... ) out there that could be of use.
You could wrap buf = sock.recv(xxx) in a data counter and sleep loop
so that you burst to no more than 50KB/s average.
Completely untestest and for illustration only :)
file_obj = open('downloaded.ext', 'wb')
interval = 1.0 # seconds eg. 0.5 or 2.0
# smaller the interval, the less bursty and smoother the throughput
max_speed = 51200 # 50k * 1024 = bytes
data_count = 0 # keep track of the amount of data transferred
time_next = time.time() + interval
while 1:
buf = sock.recv(512) # smaller chunks = smoother, more accurate
if len(buf) == 0:
break
data_count += len(buf)
if data_count >= max_speed * interval:
data_count = 0
sleep_for = time_next - time.time()
if sleep_for 0:
time.sleep(sleep_for)
time_next = time.time() + interval
file_obj.write(buf)
file_obj.close()
sock.close()