user3610137 user3610137 - 5 months ago 23
Ruby Question

Skipping unresponsive host using net/http

I am using net/http to send a bunch of request to some internal IP addresses.

Here's a snippet of the code:

File.open("internalcorpIPs", "r") do |f|
f.each_line do |line|
puts line
res = Net::HTTP.get_response(URI.parse(line))
getCode = res.code
end
end


I'm strictly just making a request to http://IP and https://IP but it seems like this method only works if every single IP/line address is live. How do I skip IP addresses with no webserver (or 80/443 ports)?

Is it possible to make it read the line, and move on to the next if no response code was returned?

Thanks!

Answer

You could simply wrap your request in begin/rescue block like this:

  File.open("internalcorpIPs", "r") do |f|
    f.each_line do |line|
      puts line
      begin
        res = Net::HTTP.get_response(URI.parse(line))
        getCode = res.code
      rescue Timeout::Error => e
        puts e
        false
      end
    end
  end

But, you will wait for 60 seconds at least before going in timeout, so I suggest to decrease the timeout value like this:

File.open("internalcorpIPs", "r") do |f|
  f.each_line do |line|
    puts line
    uri = URI.parse(line)
    http = Net::HTTP.new(uri.host, uri.port)
    http.open_timeout = 1 # 1 second
    http.read_timeout = 1 # 1 second
    begin
      response = http.start do |http|
         response = http.request_get(path = '/')
         puts response.code
      end
    rescue Timeout::Error => e
      puts e
      false
    end
  end
end

Additional notes:

Open Timeout

Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the HTTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is 60 seconds.

Read Timeout

Number of seconds to wait for one block to be read (via one read(2) call). Any number may be used, including Floats for fractional seconds. If the HTTP object cannot read data in this many seconds, it raises a Net::ReadTimeout exception. The default value is 60 seconds.

source Net::HTTP Api

hope it helps!

Comments