下载时限制 wget 或 curl 的下载速度

Gau*_*tam 128 networking download wget curl

是否可以节流(限制)wget或的下载速度curl

下载时是否可以更改节流值?

Ulr*_*gel 168

是的 wget 和 curl 都支持限制您的下载速度。手册页中直接提到了这两个选项。

卷曲

   --limit-rate <speed>
          Specify the maximum transfer rate you want curl to use. 
           This feature is useful  if you  have a limited pipe and 
           you'd like your transfer not to use your entire bandwidth.

          The given speed is measured in bytes/second, unless a suffix 
          is appended.  Appending  'k'  or 'K' will count the number
          as kilobytes, 'm' or M' makes it megabytes, while 'g' or 'G' 
          makes it gigabytes. Examples: 200K, 3m and 1G.
Run Code Online (Sandbox Code Playgroud)

例如: curl --limit-rate 423K

获取

   --limit-rate=amount
       Limit the download speed to amount bytes per second.  Amount may
       be expressed in bytes, kilobytes with the k suffix, or 
       megabytes with the m suffix.  For example, --limit-rate=20k will limit 
       the retrieval rate to 20KB/s.  This is useful when, for
       whatever reason, you don't want Wget to consume 
       the entire available bandwidth.
Run Code Online (Sandbox Code Playgroud)

例如: wget --limit-rate=423k

  • @GautamK 对于一个大文件,如果服务器接受它,您可以终止`wget` 或`curl` 进程并使用`wget -c` 或`curl -C` 恢复。如果您确实需要重新配置正在运行的进程,请使用带有守护进程的 [trickle](http://monkey.org/~marius/pages/?page=trickle) — 但设置有点复杂。或者,查看流量整形——同样,如果设置很复杂。 (16认同)
  • 是否可以在下载过程中动态更改它? (2认同)
  • @GautamK 不,因为`wget` 和`curl` 都不是交互式程序。 (2认同)

小智 7

两年后我会抛出这么一个小节目中,而wgetcurl没有互动,至少wget(以及可能的curl,但我不肯定知道)具有-c开关(代表从我离开的下载较早的地方继续)。因此,如果您需要在下载过程中更改速度,并且您大概使用了-c带有 的开关,--limit-rate=x那么您可以停止wget并以不同的速度重新启动它,它会改变。


小智 5

可以使用tcnetem工具限制流量速率,但这将限制计算机网络接口的速率。我假设您只使用wgetcurl没有其他应用程序通过网络接口交换流量。

tc 使用令牌桶过滤器(TBF)来控制速率。

TBF 的一个示例如下(参考http://www.lartc.org/manpages/tc-tbf.html):

以 0.5mbit/s 的持续最大速率、1.0mbit/s 的峰值速率、5 KB 的缓冲区附加 TBF,并计算预存储队列大小限制,因此 TBF 最多会导致 70 毫秒的延迟,并具有完美的峰值速率行为, 问题:

# tc qdisc add dev eth0 root tbf rate 0.5mbit \ burst 5kb latency 70ms peakrate 1mbit \ minburst 1540
Run Code Online (Sandbox Code Playgroud)

另一个使用 tc 和 netem 的例子如下(在http://www.linuxfoundation.org/collaborate/workgroups/networking/netem找到):

netem 学科没有内置速率控制,而是使用其他进行速率控制的学科之一。在本例中,我们使用令牌桶过滤器 (TBF) 来限制输出。

添加通过接口 eth0 的每个数据包的延迟

 # tc qdisc add dev eth0 root handle 1:0 netem delay 100ms
Run Code Online (Sandbox Code Playgroud)

以 tbf 为单位添加数据速率、数据包缓冲区大小和最大突发限制

 # tc qdisc add dev eth0 parent 1:1 handle 10: tbf rate 256kbit buffer 1600 limit 3000
Run Code Online (Sandbox Code Playgroud)

查看 tc 中为接口 eth0 分配的规则列表

 # tc -s qdisc ls dev eth0
Run Code Online (Sandbox Code Playgroud)

上述命令的输出如下

 qdisc netem 1: limit 1000 delay 100.0ms
  Sent 0 bytes 0 pkts (dropped 0, overlimits 0 )
 qdisc tbf 10: rate 256Kbit burst 1599b lat 26.6ms
  Sent 0 bytes 0 pkts (dropped 0, overlimits 0 )
Run Code Online (Sandbox Code Playgroud)

检查缓冲区和限制的选项,因为您可能会发现需要比这些更大的默认值(它们以字节为单位)