开发者

How to set limit-rate option in Curb gem?

开发者 https://www.devze.com 2023-03-31 01:50 出处:网络
I\'d like to set --limit-rate option for downloads done by Curb gem (ruby interface to curl). In curl:

I'd like to set --limit-rate option for downloads done by Curb gem (ruby interface to curl).

In curl:

curl --limit-rate 10K http://server/large_file.rar

For downloads by Curb I have this code (plus progressbar, but that's not relevant to this question):

require 'rubygems'
require 'curb'

request = 'http://server/large_file.rar'    
filename = 'large_file.rar'

f = open(filename, 'wb')

c = Curl::Easy.new(request) do |curl|    
  curl.on_body { |d| f << d; d.length }
end

c.perform

f.close

How do I set --limit-rate option in this script? As long as I can tell, there's no easy way (I'开发者_JS百科ve already read rdoc and done some googling).


You would do this by setting CURLOPT_MAX_RECV_SPEED_LARGE in libcurl. Through the curb API, you would do:

c = Curl::Easy.new(request) do |curl|
  curl.set(:max_recv_speed_large, download_limit)
  curl.on_body { |d| f << d; d.length }
end

Where download_limit is an integer for the maximum download rate in bytes per second.

For more info: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTMAXRECVSPEEDLARGE

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号