2

Is there any Perl module available for download throttling? I would like to download a certain file but limit the download rate to a specific number of KB/sec .

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

6

Looks like WWW::Curl and the CURLOPT_MAX_RECV_SPEED_LARGE option is what you want:

#!/usr/bin/env perl

use strict;
use warnings;
use feature ':5.10';
use WWW::Curl::Easy;

# Setting the options
my $curl = WWW::Curl::Easy->new;

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://www.google.com');
$curl->setopt(CURLOPT_MAX_RECV_SPEED_LARGE, 1);

my $response_body;
open my $fh, ">", \$response_body or die; # presumably this can be a real file as well.
$curl->setopt(CURLOPT_WRITEDATA,$fh);

my $ret = $curl->perform;
die 'Error: '. $curl->strerror($ret) if $ret;

my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
say "Received response: $response_body";

In this example, we download Google at one byte per second. Very slow.

jrockway
  • 42,082
  • 9
  • 61
  • 86
3

A technique not limited to Perl and not limited to a particular protocol is to use trickle:

trickle is a portable lightweight userspace bandwidth shaper. It can run in collaborative mode (together with trickled) or in stand alone mode.

See also How do you throttle the bandwidth of a socket connection in C?

It would be nice to package up this technique as a Perl module (e.g. that subclasses IO::Handle) but I am not aware of one.

Community
  • 1
  • 1
Emil Sit
  • 22,894
  • 7
  • 53
  • 75