2017-10-04 60 views
0

我需要從紅寶石運行下面的代碼:如何在ruby中獲得wget輸出?

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

當我運行這個命令我看到

輸出重定向到「wget的日誌」。

我需要做tail -f wget-log看到進步

我如何可以看到終端wget的輸出,我運行紅寶石進程?

我已經試過

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

,但它並沒有幫助

也許還有其他選擇下載大型檔案紅寶石,看看進展如何?

回答

1

您可以使用位於Ruby標準庫中的Open3模塊。

這個授予您訪問stdout,stderr,退出代碼和一個線程以等待子進程運行另一個程序時

因此,擁有一個pwd命令,你可以這樣做:

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

'Open3.capture3'沒有用的wget爲我工作。它使用'Open3.capture2'('wget http://example.org/some/large/archive.zip')' – Hirurg103

+0

是的,稍有不同。 –

0

紅寶石提供內置的庫做出任何種類的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 

隨着wget你無法捕捉任何類型的HTTP錯誤的Ruby代碼。*

隨着wget你無法進行身份驗證,基本例如Ruby代碼。*

使用Ruby內置的庫,它是非常強大的。

您可以但是有一些bug的代碼