0

Here is a URL

http://192.168.1.2:1218/?name=verify_code_string_queue&opt=get&auth=verify_code_string_queue

It will return a string or a status code like SQS_GET_END

Now I need to break the loop when the url return a string,or keep blocking.

Here is my code

require 'net/http'
require 'open-uri'
loop do
  codeText = open("http://192.168.1.2:1218/?name=verify_code_string_queue&opt=get&auth=verify_code_string_queue") do |repo|
    repo.read
  end
  if codeText != "SQS_GET_END"
    break
  end
end

But it doesn't work,output cannot assign requested address - connect(2) (Errno::EADDRNOTAVAIL). Please tell me how to solve it,thanks

ZhuRong
  • 1
  • 2

1 Answers1

0

The problem is that you are doing too many requests in a short time. This will result in the Errno::EADDRNOTAVAIL error. You can add a sleep within the loop, to limit the number of requests you make.

Something like this:

require 'net/http'
require 'open-uri'
loop do
  codeText = open("http://192.168.1.2:1218/?name=verify_code_string_queue&opt=get&auth=verify_code_string_queue") do |repo|
    repo.read
  end
  if codeText != "SQS_GET_END"
    break
  end
  sleep 1 # <= sleep for one second
end
rdvdijk
  • 4,400
  • 26
  • 30