class GetProxyList::Base

Attributes

save_path[W]

To change this template use File | Settings | File Templates.

Public Class Methods

new(save_path=nil) click to toggle source
# File lib/get_proxy_list/base.rb, line 9
def initialize(save_path=nil)
  @save_path = save_path
  unless @save_path
    @save_path = File.expand_path("../proxylist", __FILE__)
  end
end

Public Instance Methods

get_proxy_in_time_limit(limit,proxylist) click to toggle source

获取符合时限的代理 limit 时限 proxylist 待筛选的代理列表

# File lib/get_proxy_list/base.rb, line 20
def get_proxy_in_time_limit(limit,proxylist)
  limit_list = []
  proxylist.each do |proxy|
    url = "http://" + proxy["ip"].to_s + ":" + proxy["port"].to_s
    time_start = Time.now.to_i
    begin
      timeout(limit+1) do
        doc = Nokogiri::HTML(open("http://www.baidu.com",:proxy=> url))
        x = doc.css("em")
      end
      time_end = Time.now.to_i
      time_use = time_end - time_start
      p  "#{url}   use_time:#{time_use}"
    rescue Exception =>e
      case e
        when Errno::ETIMEDOUT
          p "Use #{url} timeout"
        when Timeout::Error
          p "Use #{url} timeout"
        when Errno::ECONNREFUSED
          p "Use #{url} Error connection"
        else
          p "Use #{url} Error:#{e.to_s}"
      end
      time_use = -1
    end
    if(time_use>=0&&time_use<=limit)
      limit_list << url
    end
  end
  limit_list
end