EOFError:达到文件结尾 Net::HTTP 问题
我正在使用 ruby-1.8.7-p302/Rails 2.3.11。我正在尝试使用 FQL(Facebook API)来获取链接的统计信息。这是我的代码:
def stats(fb_post_url)
url = BASE_URI + "?query=#{URI.encode("select like_count from link_stat where url=\"#{fb_post_url}\"")}"
parsed_url = URI.parse(url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
request = Net::HTTP::Get.new(parsed_url.request_uri)
response = http.request(request)
response.inspect
end
这是错误:
EOFError: end of file reached
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:67:in `timeout'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/timeout.rb:101:in `timeout'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:2017:in `read_new'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1051:in `request'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1037:in `request'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:543:in `start'
from /home/rahul/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/net/http.rb:1035:in `request'
from /home/rahul/Work/Radr/lib/fb_stats.rb:13:in `stats'
from (irb):10
这似乎只在 Facebook API 的情况下发生。另外,我在一些帖子中看到它建议这可能是 Net::HTTP 中的一个错误。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果 URL 使用 https 而不是 http,则需要添加以下行:
请注意附加的
http.use_ssl = true
。处理 http 和 https 的更合适的代码将类似于以下代码。
在我的博客中查看更多内容: EOFError:使用 Net::HTTP 发布表单时出现文件结尾问题。
If the URL is using https instead of http, you need to add the following line:
Note the additional
http.use_ssl = true
.And the more appropriate code which would handle both http and https will be similar to the following one.
See more in my blog: EOFError: end of file reached issue when post a form with Net::HTTP.
我对非 SSL 服务的请求也遇到了类似的问题。
该博客含糊地建议尝试对传递给“get”的 URL 进行 URI 编码: http://www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html
我出于绝望而尝试了它,并且在我的限制测试中,这似乎已经为我解决了。我的新代码是:
请注意,我使用 @http.start 因为我想在多个请求上维护 HTTP 会话。除此之外,您可能想尝试最相关的部分: get 调用中的 URI.encode(url)
I had a similar problem with a request to a non-SSL service.
This blog suggested vaguely to try URI encoding the URL that is passed to 'get': http://www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html
I took a shot at it, based on desperation, and in my limiting testing this seems to have fixed it for me. My new code is:
Note that I use @http.start as I want to maintain the HTTP session over multiple requests. Other than that, you might like to try the most relevant part which is: URI.encode(url) inside the get call
我发现我会定期遇到这样的 Net::HTTP 和 Net::FTP 问题,当我这样做时,用 timeout() 包围调用会使所有这些问题消失。因此,它偶尔会挂起 3 分钟左右,然后引发 EOFError:
这总是为我修复它:
I find that I run into Net::HTTP and Net::FTP problems like this periodically, and when I do, surrounding the call with a timeout() makes all of those issues vanish. So where this will occasionally hang for 3 minutes or so and then raise an EOFError:
This always fixes it for me:
我遇到了同样的问题,ruby-1.8.7-p357,并徒劳地尝试了很多事情......
我终于意识到它只发生在使用相同 XMLRPC::Client 实例的多次调用上!
所以现在我在每次调用时重新实例化我的客户端,它就可以工作了:|
I had the same problem, ruby-1.8.7-p357, and tried loads of things in vain...
I finally realised that it happens only on multiple calls using the same XMLRPC::Client instance!
So now I'm re-instantiating my client at each call and it just works:|
经过一些研究,这种情况发生在 Ruby 的
XMLRPC::Client
库中 - 它使用NET::HTTP
。客户端使用NET::HTTP
中的start()
方法来保持连接打开以供将来的请求。这恰好发生在最后一次请求后 30 秒——所以我的假设是它所访问的服务器在该时间之后关闭请求。我不确定 NET::HTTP 保持请求打开的默认值是什么 - 但我将用 60 秒进行测试,看看是否可以解决问题。
After doing some research, this was happening in Ruby's
XMLRPC::Client
library - which usesNET::HTTP
. The client uses thestart()
method inNET::HTTP
which keeps the connection open for future requests.This happened precisely at 30 seconds after the last requests - so my assumption here is that the server it's hitting is closing requests after that time. I'm not sure what the default is for
NET::HTTP
to keep the request open - but I'm about to test with 60 seconds to see if that solves the issue.我最近遇到了这个问题,最终发现这是由我们所访问的端点的网络超时引起的。对我们来说幸运的是,我们能够增加超时持续时间。
为了验证这是我们的问题(实际上不是 net http 的问题),我使用curl 发出了相同的请求,并确认该请求已被终止。
I ran into this recently and eventually found that this was caused by a network timeout from the endpoint we were hitting. Fortunately for us we were able to increase the timeout duration.
To verify this was our issue (and actually not an issue with net http), I made the same request with curl and confirmed that the request was being terminated.
在 Ruby on Rails 中,我使用了这段代码,它运行得很好:
In Ruby on Rails I used this code, and it works perfectly: