Programing

EOFError : Net :: HTTP에서 파일 끝에 도달했습니다.

lottogame 2020. 6. 16. 21:42
반응형

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의 버그 일 수 있음을 제안했습니다.


URL이 http 대신 https를 사용하는 경우 다음 행을 추가해야합니다.

parsed_url = URI.parse(url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
http.use_ssl = true

추가 사항에 유의하십시오 http.use_ssl = true.

그리고 http와 https를 모두 처리하는 더 적절한 코드는 다음 코드와 비슷합니다.

url = URI.parse(domain)
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({'name'=>'Sur Max', 'email'=>'some@email.com'})
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
response = http.request(req)

내 블로그에서 더 많은 것을보십시오 : EOFError : Net :: HTTP로 양식을 게시 할 때 파일 끝에 도달했습니다 .


SSL이 아닌 서비스에 대한 요청과 비슷한 문제가있었습니다.

이 블로그는 'get'에 전달 된 URL을 URI 인코딩하는 것을 모호하게 제안했습니다. http://www.loudthinking.org/2010/02/ruby-eoferror-end-of-file-reached.html

나는 절망에 기초하여 그것을 쏘았습니다. 제 한계 테스트에서 이것이 나를 위해 고쳐 놓은 것 같습니다. 내 새 코드는 다음과 같습니다

@http = Net::HTTP.new('domain.com')  
@http = @http.start    
url = 'http://domain.com/requested_url?blah=blah&etc=1'
req = Net::HTTP::Get.new(URI.encode(url))
req.basic_auth USERNAME, API_KEY
res = @http.request(req) 

여러 요청에 대해 HTTP 세션을 유지하려면 @ http.start를 사용하십시오. 그 외에는 get call 내부에서 URI.encode (url) 과 가장 관련이있는 부분을 시도하고 싶을 수도 있습니다.


Net :: HTTP 및 Net :: FTP 문제가 주기적으로 발생한다는 것을 알았습니다. 그렇다면 timeout () 호출을 둘러싼 모든 문제가 사라집니다. 따라서 때때로 3 분 정도 멈추고 EOFError가 발생합니다.

res = Net::HTTP.post_form(uri, args)

이것은 항상 나를 위해 수정합니다.

res = timeout(120) { Net::HTTP.post_form(uri, args) }

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:|


After doing some research, this was happening in Ruby's XMLRPC::Client library - which uses NET::HTTP. The client uses the start() method in NET::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.


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.


In Ruby on Rails I used this code, and it works perfectly:

req_profilepic = ActiveSupport::JSON.decode(open(URI.encode("https://graph.facebook.com/me/?fields=picture&type=large&access_token=#{fb_access_token}")))

profilepic_url = req_profilepic['picture']

참고URL : https://stackoverflow.com/questions/5244887/eoferror-end-of-file-reached-issue-with-nethttp

반응형