简体   繁体   English

如何使用'net / http'向本地主机发送请求失败,并到达文件尾(EOFError)

[英]How to send a request to localhost using 'net/http' fails with end of file reached (EOFError)

I'm using Ruby version 2.3.0. 我正在使用Ruby 2.3.0版。

I want to check when my application is up, and I wrote this method for my "deployer". 我想检查我的应用程序何时启动,并且为“部署程序”编写了此方法。 At runtime http.request_get(uri) raises 在运行时http.request_get(uri)引发

EOFError: end of file reached

when I pass http://localhost as a first argument into the method: 当我将http://localhost作为第一个参数传递给方法时:

require 'net/http'

def check_start_application(address, port)
  success_codes = [200, 301]
  attempts = 200
  uri = URI.parse("#{address}:#{port}")
  http = Net::HTTP.new(uri.host, uri.port)

  attempts.times do |attempt|
    # it raises EOFError: end of file reached
    http.request_get(uri) do |response|
      if success_codes.include?(response.code.to_i)
        return true
      elsif attempt == attempts - 1
        return false
      end
    end
  end
end

But, when I test this method separately from a context with irb, this code works pretty well for two cases: 但是,当我与irb上下文分开测试此方法时,此代码在两种情况下效果很好:

check_start_application('http://example.com', '80')
check_start_application('http://localhost', any_port)

In an app's context this code works for only one case: 在应用程序的上下文中,此代码仅适用于以下情况:

check_start_application('http://example.com', '80')

What I tried: 我试过的

  • using 'rest-client' instead of 'net/http' 使用“ rest-client”而不是“ net / http”
  • using 'net/https' with http.use_ssl = false http.use_ssl = false使用'net / https'
  • remove times from the method 从方法中删除times
  • call sleep before a request 在请求之前致电sleep

Who faced a similar problem? 谁遇到过类似的问题? I believe I'm not the only one. 我相信我不是唯一的一个。

It may be that on the deploy your app is running on SSL. 可能是在部署中,您的应用程序正在SSL上运行。 It's hard to help debug without access to it, but try with: 如果无法访问它,很难帮助调试,但是请尝试:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM