本文介绍了在 RoR 中,如何从“无法打开 TCP 连接...(一般 SOCKS 服务器故障)"中恢复?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Rails 4.2.7.目前我正在通过像这样的 SOCKS 代理请求网页

I’m using Rails 4.2.7. Currently Im’ requesting web pages through a SOCKS proxy like so

begin
   ...
    res1 = Net::HTTP.SOCKSProxy('127.0.0.1', 50001).start(uri.host, uri.port) do |http|
      puts "launching #{uri}"
      resp = http.get(uri)
      status = resp.code
      content = resp.body
      content_type = resp['content-type']
      content_encoding = resp['content-encoding']
    end
  ...
rescue OpenURI::HTTPError => ex
   ...
rescue SocketError, Net::OpenTimeout, Zlib::BufError => e
   ...
end

偶尔我会收到以下错误..

and occasionally I will get the following error ..

Error during processing: Failed to open TCP connection to otherwebsite.com:80 (general SOCKS server failure)
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:879:in `block in connect'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:101:in `timeout'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:878:in `connect'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:863:in `do_start'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:852:in `start'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:584:in `start'
/Users/mikeb/Documents/workspace/runtrax/app/helpers/webpage_helper.rb:96:in `get_content'
/Users/mikeb/Documents/workspace/runtrax/app/helpers/webpage_helper.rb:32:in `get_url'
/Users/mikeb/Documents/workspace/runtrax/app/services/onlinerr_race_finder_service.rb:42:in `get_race_list'
/Users/mikeb/Documents/workspace/runtrax/app/services/abstract_race_finder_service.rb:26:in `process_data'
/Users/mikeb/Documents/workspace/runtrax/app/services/run_crawlers_service.rb:18:in `block in run_all_crawlers'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each'

如何捕获此错误并重试我的请求?

How do I catch this error and retry my request?

我的问题与问的有什么不同?重复是指缓冲区错误,此问题处理套接字错误.

How is my question different than what was asked? The duplicate is referring to a buffer error, this quesiton deals with a socket error.

推荐答案

您可以 重试:

retries = 0
begin
  res1 = Net::HTTP.SOCKSProxy('127.0.0.1', 50001).start(uri.host, uri.port) do |http|
    puts "launching #{uri}"
    resp = http.get(uri)
    status = resp.code
    content = resp.body
    content_type = resp['content-type']
    content_encoding = resp['content-encoding']
  end
rescue => e
  retries += 1
  if retries < 3
    retry # <-- Jumps to begin
  else
    # Error handling code, e.g.
    logger.warn "Couldn't connect to proxy: #{e}"
  end
end

这篇关于在 RoR 中,如何从“无法打开 TCP 连接...(一般 SOCKS 服务器故障)"中恢复?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:38