user3081809
user3081809

Reputation: 105

check proxy and get timout with ruby-on-rails

I have ruby on rails 4. How I can to check proxy and get information abot this proxy (timeout and etc.), if it work?

I parse page with nokoriri through proxy.

page = Nokogiri::HTML(open("http://bagche.ru/home/radio_streem/", :proxy => "http://213.135.96.35:3129", :read_timeout=>10))

Upvotes: 1

Views: 1027

Answers (1)

user3081809
user3081809

Reputation: 105

gem install curb

  require 'net/http'
  require 'net/ping'
  require 'curb'

  def proxy_check
    @proxies = Proxy.all
    url = "ya.ru"
    @proxies.each do |p|
      proxy = Net::Ping::TCP.new(p.proxy_address, p.proxy_port.to_i)
      if proxy.ping?
        @resp = Curl::Easy.new(url) { |easy|
          easy.proxy_url = p.proxy_address
          easy.proxy_port=p.proxy_port.to_i
#          easy.timeout=90
#          easy.connect_timeout=30
          easy.follow_location = true
          easy.proxy_tunnel = true
        }
        begin
        @resp.perform
        @resp.response_code
        rescue
          puts "CURL_GET -e- fail "+p.proxy_address
          if @resp.response_code == 200
            p.proxy_status = 1
            p.proxy_timeout = @resp.total_time
          else
            p.proxy_status = 0
            puts "CURL_GET  fail "+p.proxy_address
          end
        end
      else
        p.proxy_status = 0
        puts "ping fail "+p.proxy_address
      end
      p.save
    end
  end

returned 200 if availabil.

Upvotes: 2

Related Questions