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

发布于 2024-07-13 16:56:09 字数 58 浏览 3 评论 0原文

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

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-07-20 16:56:09

看起来像 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";

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

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.

旧时浪漫 2024-07-20 16:56:09

不限于 Perl 且不限于特定协议的技术是使用 trickle

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

另请参阅如何限制带宽C 中的套接字连接?

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文