如何在 ruby​​ 中使用 net/http 处理超时,同时向给定 IP 范围内的 IP 发送请求,并跳过超时的 IP 并移至下一个?

发布于 2025-01-07 12:33:51 字数 1391 浏览 2 评论 0原文

我想处理从控制台获取的 IP 范围的超时,我向获取范围内的 IP 发出请求并收到超时错误。 我想向所有 IP 发出请求并获得它们的响应。 对于超时的IP,想跳过它并移至下一个。如何处理这个问题,以便循环不会出现异常,并且脚本将请求发送到所有可以提供响应处理超时的 IP。

在此附加代码:

require 'net/http'
require 'uri'
require 'ipaddr'

puts "Origin IP:"
originip = gets()
(IPAddr.new("209.85.175.121")..IPAddr.new("209.85.175.150")).each do |address|
  req = Net::HTTP.get(URI.parse("http://#{address.to_s}"))
  puts req
end

错误:

C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `initialize': A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. - connect(2) (Errno::ETIMEDOUT)
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `open'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:53:in `timeout'
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:553:in `do_start'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:542:in `start'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:379:in `get_response'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:356:in `get'
        from IP Range 2.rb:9
        from IP Range 2.rb:8:in `each'

I want to handle timeout for IP range taken from console for which I make requests to IPs within taken range and getting timeout error.
I want to make requests to all IPs and get responses from them.
For IP that time out , want to skip it and move to next one. How to handle this so loop dont get exception and script sends request to all IPs that can give response handling timed out ones.

Attaching code here:

require 'net/http'
require 'uri'
require 'ipaddr'

puts "Origin IP:"
originip = gets()
(IPAddr.new("209.85.175.121")..IPAddr.new("209.85.175.150")).each do |address|
  req = Net::HTTP.get(URI.parse("http://#{address.to_s}"))
  puts req
end

Error:

C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `initialize': A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. - connect(2) (Errno::ETIMEDOUT)
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `open'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:53:in `timeout'
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:553:in `do_start'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:542:in `start'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:379:in `get_response'
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:356:in `get'
        from IP Range 2.rb:9
        from IP Range 2.rb:8:in `each'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2025-01-14 12:33:51

正如马克所说。你应该拯救这个异常。像这样:

begin
  response = Net::HTTP.get(...)
rescue Errno::ECONNREFUSED => e
  # Do what you think needs to be done
end

此外,从调用 get() 中返回的是响应,而不是请求。

Just like Marc says. You should rescue the exception. Like so:

begin
  response = Net::HTTP.get(...)
rescue Errno::ECONNREFUSED => e
  # Do what you think needs to be done
end

Also, what you get back from the call to get() is a response, not a request.

世界如花海般美丽 2025-01-14 12:33:51

使用 timeout 捕获异常,

require 'timeout'

(IPAddr.new("209.85.175.121")..IPAddr.new("209.85.175.150")).each do |address|
  begin
    req = Net::HTTP.get(URI.parse("http://#{address.to_s}"))
    puts req
  rescue Timeout::Error => exc
    puts "ERROR: #{exc.message}"
  rescue Errno::ETIMEDOUT => exc
    puts "ERROR: #{exc.message}"
  # uncomment the following two lines, if you are not able to track the exception type.
  #rescue Exception => exc
  #  puts "ERROR: #{exc.message}"
  end
end

编辑:当我们拯救Timeout::Error,只有那些属于Timeout::Error类的异常才会被捕获。我们需要使用它们的错误类捕获引发的异常,并相应地更新代码。

Catch the exception using timeout,

require 'timeout'

(IPAddr.new("209.85.175.121")..IPAddr.new("209.85.175.150")).each do |address|
  begin
    req = Net::HTTP.get(URI.parse("http://#{address.to_s}"))
    puts req
  rescue Timeout::Error => exc
    puts "ERROR: #{exc.message}"
  rescue Errno::ETIMEDOUT => exc
    puts "ERROR: #{exc.message}"
  # uncomment the following two lines, if you are not able to track the exception type.
  #rescue Exception => exc
  #  puts "ERROR: #{exc.message}"
  end
end

Edit: When we rescue Timeout::Error, only those exceptions which belongs to Timeout::Error class will be caught. We need to catch the raised exception using their error class, updated the code accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文