使用NET :: HTTP下载文件时,有没有办法获得当前的下载速度?
答案 0 :(得分:0)
require 'open-uri'
time = Time.now
stuff = open('http://www.giovannicapuano.net').read
File.write 'stuff', stuff
time_spent = Time.now - time
file_size = File.size('stuff') / 1024.0 / 1024.0
speed = file_size / time_spent
...或
require 'net/http'
time = Time.now
uri = URI 'http://www.giovannicapuano.net'
stuff = Net::HTTP.get_response(uri).body
File.write 'stuff', stuff
time_spent = Time.now - time
file_size = File.size('stuff') / 1024.0 / 1024.0
speed = file_size / time_spent