Limitate speed of a socket-based data transferring

Diez B. Roggisch deets at nospam.web.de
Thu Sep 14 08:10:05 EDT 2006


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.


Diez



More information about the Python-list mailing list