In-Reply-To: [ruby-list:6560] http_get.rb

前回の物に、% の計算間違いというミスがありました。また、表示がひたすら
流れていってしまうのはあまりにも見にくいので、これを改善。そして、ちょっ
と離れると表示が見にくかったので、5% 単位の簡単なグラフを付けました。


#!/usr/local/bin/ruby
#
# http_get.rb http://host/target
#
# Wakou Aoyama <pxn11625 / niftyserve.or.jp>

require "socket"

host, port, file = ARGV.shift.scan(%r|http://([^:/]+):?([^/]*)(.+)|)[0]
sock = TCPsocket.open(host, (port == "" ? 80 : port))
sock.binmode

sock.print "GET #{file} HTTP/1.0\r\n\r\n"
head = ''
head += sock.read(1) until /\r\n\r\n|\n\n|\r\r/ =~ head
content_length = head.scan(/^Content-length:\s*(\d+)/i)[0][0].to_i
length = 0
start_time = Time.now

while data = sock.gets
  print data
  length += data.length
  speed = length / (Time.now - start_time)
  rest_time = (content_length - length) / speed
  per = 100 * length / content_length
  STDERR.print length, "/", content_length, " bytes ", per, " % "
  STDERR.print (("#" * (per / 5)), ("_" * (20 - per / 5)), " ")
  STDERR.print speed, " bytes/sec EndTime: "
  STDERR.print ((Time.now + rest_time).to_s.split[3], "\n")
  STDERR.print "\c[[1A"
end
if length == content_length
  STDERR.print "\n===== All End ! =====\n"
else
  STDERR.print "\n===== Connection Down ! =====\n"
end


青山 和光 Wakou Aoyama <pxn11625 / niftyserve.or.jp>