Skip to content

Instantly share code, notes, and snippets.

@mimosz
Created December 16, 2012 04:58
Show Gist options
  • Save mimosz/4303448 to your computer and use it in GitHub Desktop.
Save mimosz/4303448 to your computer and use it in GitHub Desktop.
粗放的监测代理是否有效~~
# 未知的
unkown_list = [
"219.130.39.9:3128",
"122.225.22.22:8080",
"202.103.215.199:80",
"59.46.173.75:80",
"218.23.49.155:80",
"218.76.157.98:8001",
"218.84.126.82:3128"
]
# 无效的
fail_list = []
# 有效的
proxy_list = []
# 通过访问天猫测试
def test_proxy(proxy)
body = Nestful.get 'http://err.tmall.com/error1.html', proxy: "http://#{proxy}", timeout: 15
if body
body = body.force_encoding("GBK").encode("UTF-8")
if body.match('很抱歉,您查看的页面找不到了!')
return proxy # 有效
end
end
return nil
rescue Nestful::TimeoutError, Nestful::ResourceNotFound, Errno::ECONNREFUSED, EOFError
return nil
end
# 遍历
unkown_list.each do |proxy|
proxyer = test_proxy(proxy)
if proxyer
proxy_list << proxyer
else # 无效
fail_list << proxy
end
end
puts proxy_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment