简体   繁体   English

如何获得wget在ruby中的输出?

[英]How do I get wget output in ruby?

I need to run the following code from ruby: 我需要从ruby运行以下代码:

system "wget http://example.org/some/large/archive.zip"

When I run this command I see 当我运行此命令时,我看到

Redirecting output to 'wget-log'. 将输出重定向到“ wget-log”。

I need to do tail -f wget-log to see the progress 我需要做tail -f wget-log以查看进度

How can I see wget output in terminal where I run the ruby process? 如何在运行ruby进程的终端中看到wget输出?

I've tried 我试过了

system "wget -O - http://example.org/some/large/archive.zip > dev/null"

but it didn't help 但这没有帮助

Maybe there are other options to download large archives with ruby and see the progress? 也许还有其他选择可以下载带有ruby的大型档案并查看进度?

You could use the Open3 module, which is in the Ruby's standard library. 您可以使用Ruby的标准库中的Open3模块。

This grants you access to stdout, stderr, exit codes and a thread to wait for the child process when running another program . 这使您可以访问stdout,stderr,退出代码和运行另一个程序时等待子进程的线程

So, having a pwd command you can do something like: 因此,使用pwd命令,您可以执行以下操作:

require 'open3'
stdout, stderr, status = Open3.capture3('pwd')
puts stdout # ~/ current directory
puts stderr #    no error
puts status # pid 25522 exit 0

The ruby provide a good built-in libs to make any kind of http things, for example to download file you can use net/http : ruby提供了一个很好的内置库来制作任何类型的http东西,例如下载文件,您可以使用net/http

require 'net/http'
require 'uri'

uri = URI(URI.encode("http://example.org/some/large/archive.zip"))
Net::HTTP.start(uri.host,uri.port) do |http|
  request = Net::HTTP::Get.new uri.path

  http.request request do |response|
    open "/tmp/my_large_file.zip", 'w' do |io|
       response.read_body do |chunk|
         puts "Writing  #{chunk.length} bits ..." # see progress
         io.write chunk
       end
     end
  end  
end

With wget you unable to catch any kind of http errors in your ruby code.* 使用wget 您将无法在ruby代码中捕获任何形式的http错误。*

With wget you unable to make auth, basic for example in your ruby code.* 使用wget 您将无法进行身份验证,例如在ruby代码中进行基本认证。*

Use Ruby built-in libs, it is very powerfull. 使用Ruby内置库,它非常强大。

You able but with some buggy code 您可以但有一些错误代码

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

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