如何限制Perl程序中的下载?

时间:2023-01-15 20:46:40

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 .

是否有可用于下载限制的Perl模块?我想下载某个文件,但将下载速率限制为特定的KB /秒数。

2 个解决方案

#1


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

看起来像WWW :: Curl和CURLOPT_MAX_RECV_SPEED_LARGE选项是你想要的:

#!/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.

在此示例中,我们以每秒一个字节的速度下载Google。非常慢。

#2


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

不限于Perl并且不限于特定协议的技术是使用涓流:

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

trickle是一种便携式轻量级用户空间带宽整形器。它可以在协作模式下运行(与滴流一起)或在独立模式下运行。

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

另请参见如何在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.

将这种技术打包为Perl模块(例如,子类IO :: Handle)会很好,但我不知道。

#1


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

看起来像WWW :: Curl和CURLOPT_MAX_RECV_SPEED_LARGE选项是你想要的:

#!/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.

在此示例中,我们以每秒一个字节的速度下载Google。非常慢。

#2


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

不限于Perl并且不限于特定协议的技术是使用涓流:

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

trickle是一种便携式轻量级用户空间带宽整形器。它可以在协作模式下运行(与滴流一起)或在独立模式下运行。

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

另请参见如何在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.

将这种技术打包为Perl模块(例如,子类IO :: Handle)会很好,但我不知道。