gpt4 book ai didi

ssl - 机械化获取 "Errno::ECONNRESET: Connection reset by peer - SSL_connect"

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:26 26 4
gpt4 key购买 nike

我无法让 Mechanize 加载一个曾经工作过的页面——它可靠地失败了,并出现 Errno: ECONNRESET: Connection reset by peer - SSL_connect 消息。关于我应该尝试什么或我应该查看的细节有什么建议吗? (请参阅下面的“我尝试过的内容”...)

更新1

a related S.O. post 得到提示,我尝试直接使用 Net::HTTP 访问该站点。当我设置 http.ssl_version = :TLSv1 时,我得到了重定向而不是错误(它应该是)。所以我的问题变成了:如何从 Mechanize 中配置 Net::HTTP 的底层 ssl_version 参数?

谢谢...

症状:

$ rails console
>> a = Mechanize.new
=> #<Mechanize:0x007fd26789b8e0 ...>
>> p = a.get("http://sce.com")
# (...after a long pause...)
Errno::ECONNRESET: Connection reset by peer - SSL_connect
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /sandbox/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:857:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:691:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:631:in `connection_for'
...
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:257:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:974:in `response_redirect'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:298:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize.rb:432:in `get'
from (irb):3

环境:

$ rake about
About your application's environment
Ruby version 2.0.0 (x86_64-darwin12.4.0)
RubyGems version 2.1.9
Rack version 1.5
Rails version 4.0.0
JavaScript Runtime JavaScriptCore
Active Record version 4.0.0
Action Pack version 4.0.0
Action Mailer version 4.0.0
Active Support version 4.0.0
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd423c50e0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
Application root /Users/me/MyProject
Environment development
Database adapter postgresql
Database schema version 20131017201057

$ openssl version
OpenSSL 1.0.1e 11 Feb 2013

$ system_profiler -detailLevel mini SPSoftwareDataType

System Software Overview:

System Version: OS X 10.8.5 (12F45)
Kernel Version: Darwin 12.5.0
Time since boot: 8 days 7:40

我尝试过的:

  • 我在 Firefox 网络浏览器中尝试了相同的 URL。它有效。
  • 我已明确设置 Mechanize request_headers 以完全模仿 Firefox 浏览器。没有变化。
  • 我已经按照 this S.O post 中的描述更新了我的 ssl/cert.pem 文件(第二次) .没有变化。
  • 我试过不同的网站:www.pge.comwww.sdge.com。两者都有效。 www.sce.com 网站有些不同。

Net::HTTP 成功

这是一个 Net::HTTP 工作的例子:

$ irb
>> require 'net/https'
=> true
>> require 'uri'
=> false
>> uri = URI.parse("https://www.sce.com/")
=> #<URI::HTTPS:0x007facab8f6ba0 URL:https://www.sce.com/>
>> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP www.sce.com:443 open=false>
>> http.use_ssl = true
=> true
>> http.ssl_version = :TLSv1 # <= this line makes all the difference
=> :TLSv1
>> r = http.start { |agent| p agent.get(uri.path) }
=> #<Net::HTTPFound 302 Found readbody=true>
>> r.to_hash
=> {"content-language"=>["en-US"], "date"=>["Fri, 18 Oct 2013 01:00:07 GMT"], "location"=>["https://www.sce.com/wps/portal/home/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOIt3Q1cPbz8DTzdQwKNDTyNAw38gh0djQ0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgAgIrzaA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/"], "p3p"=>["CP=\"NON CUR OTPi OUR NOR UNI\""], "server"=>["IBM_HTTP_Server"], "transfer-encoding"=>["chunked"], "x-powered-by"=>["Servlet/3.0"], "set-cookie"=>["PD_STATEFUL_d55ece64-8d9a-11e2-84a1-0050560010d6=%2Fwps; Path=/", "session_www=740796608.47873.0000; path=/"]}

最佳答案

Mechanize 邮件列表中的人们友善地提供了答案:

agent = Mechanize.new do |a|
a.ssl_version = :TLSv1
end

关于ssl - 机械化获取 "Errno::ECONNRESET: Connection reset by peer - SSL_connect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19439506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com