ruby - 为什么 IO.popen 卡在有效命令上?

标签 ruby curl stream popen

我正在使用 IO.popen 运行 curl 命令(八位字节流 POST)。我复制命令并在命令行上运行它并且它有效。然而,使用 popen 在 Ruby 中运行它会导致流在我的服务器上意外结束并且 Ruby 脚本挂起,curl 无休止地运行。

这是我正在做的:

curl = "curl "
curl += "--cookie \"JSESSIONID=#{@sessionid}\" "
curl += "-H \"Content-type:application/octet-stream\" "
curl += "-X POST "
curl += "-o \"#{responseFile}\" "
curl += "--data-binary \"@#{filename}\" "
curl += "\"#{url}\""

puts "command: #{curl}"

IO.popen(curl, "r") { |out|
  out.readlines
}
# Script never makes it to here

我一定是在做傻事。这是什么?

Here is the stdout from curl.

我正在使用 Ruby 1.8.6

最佳答案

popen 还为 curl 提供了一个管道以从 读取数据,如本例所示:

$ echo -e 'foo\nbar\nzoo' | \
  ruby -e 'pipe = IO.popen("grep oo", "r") {|out| p out.readlines}'
["foo\n", "zoo\n"]

我猜 curl 也想从 stdin 向服务器发送 POST,但那里什么也没有,所以超时 30 秒。

关于ruby - 为什么 IO.popen 卡在有效命令上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302951/

相关文章:

ruby - 将 Rack-Jekyll 应用程序推送到 Heroku

php - 使用 CURL 检查网站是否使用 SSL

php - 如何在 Wordpress 上启用 curl

c++ - 在 GCC 4.7.2 和 Clang 3.2 中显然缺少 getline() 的重载,将 RRef 流式传输

python - HDI : write large string xml into file (python xml. dom.minidom)

ruby-on-rails - 未从 css 文件 Heroku 和 S3 引用 Rails 4 供应商 Assets 图像

ruby-on-rails - 直接链接(无重定向)到 ActiveStorage 中的文件

ruby-on-rails - 查找日期范围内的记录

php - WAMP Curl 安装

stream - 无缓冲 CXF(无内容长度 header )