Ruby 读取文件中的 url 地址出现 EOFError

chen248283935 · 2013年07月30日 · 最后由 chen248283935 回复于 2013年08月02日 · 4429 次阅读

文件存放 url 地址,依次读取,正常的测试:输出每一行,是没问题的。 但采用下述代码在 41 行的时候出现 EOFError 错误 查看了一下,uri 和 http 建立正常 在 response = http.request request 出错

是什么原因呢

ruby:2.0.0.0p

@file.each_line do |line|
        count +=1 
        puts "count:#{count} #{line}"
        url = line.chomp
        uri=URI(url)
        puts url.unpack("H*")
        uri += "/web.config"
        begin
        http = Net::HTTP.start(uri.host,uri.port)
        request = Net::HTTP::Get.new uri 
        response = http.request request
        status = response.code
        if( "404" != status ) then
                puts "#{status} || #{uri}"
        end 
        rescue Errno::ETIMEDOUT
                puts "error:ETIMEOUT"
        end 
end 

错误提示:

count:41 http://www.ahkytb.com 687474703a2f2f7777772e61686b7974622e636f6d /home/april/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': end of file reached (EOFError)

和文件无关 http://www.ahkytb.com/web.config 主机直接关闭连接了,返回内容长度为 0, 这个 EOFError 是 net/http 按照协议读响应而产生的

begin
  http = Net::HTTP.start(uri.host,uri.port)
  request = Net::HTTP::Get.new uri 
  response = http.request request
  status = response.code
  if( "404" != status ) then
          puts "#{status} || #{uri}"
  end 
rescue Errno::ETIMEDOUT
          puts "error:ETIMEOUT"
rescue EOFError
          puts "host closed connection"
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号