2012-10-22 17 views
1

我想使用系統命令curl保存頁面的內容,但它不起作用。 我認爲這是本地主機的問題,但我不知道如何解決這個問題。Ruby on Rails:在localhost中使用Curl命令

def save_page 
    `/usr/bin/curl -O http://127.0.0.1:3000/category_plist` 
end 

服務器輸出:

% Total % Received % Xferd Average Speed Time Time  Time Current 
          Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- 0:04:54 --:--:--  0 
+1

你curl命令將反引號裏面工作,如果URL是正確的。因爲發生的所有事情都是正在創建的子shell,並且正在執行Curl,所以您應該能夠在終端窗口中執行完全相同的命令並獲得相同的結果。我會先從終端窗口調試問題,然後Ruby中的子shell應該可以正常工作。 –

回答

1

如果你改變你的函數如下它應該工作...

def save_page 
    system("/usr/bin/curl -O http://127.0.0.1:3000/category_plist") 
end 

爲了測試命令就是這樣,退出你的本地網絡服務器和運行「軌道控制檯」,從那裏你可以輸入這樣的命令,並得到一個即時的迴應...

$ rails console 
Loading development environment (Rails 3.1.0.rc4) 
1.9.2-p290 :001 > puts system("ls -l") 
total 56 
[email protected] 1 creativetechnologist staff 212 3 Feb 2011 Capfile 
[email protected] 1 creativetechnologist staff 924 6 Feb 2012 Gemfile 
[email protected] 1 creativetechnologist staff 3072 6 Feb 2012 Gemfile.lock 
[email protected] 1 creativetechnologist staff 3943 16 Oct 23:16 MOR_git.tmproj 
[email protected] 1 creativetechnologist staff 18 12 Jul 19:06 README 
[email protected] 1 creativetechnologist staff 267 30 Aug 2011 Rakefile 
drwxr-xr-x 7 creativetechnologist staff 238 30 Aug 2011 app 
drwxr-xr-x 11 creativetechnologist staff 374 13 Sep 2011 config 
[email protected] 1 creativetechnologist staff 157 30 Aug 2011 config.ru 
drwxr-xr-x 6 creativetechnologist staff 204 3 Feb 2011 db 
drwxr-xr-x 3 creativetechnologist staff 102 3 Feb 2011 doc 
drwxr-xr-x 4 creativetechnologist staff 136 3 Feb 2011 key 
drwxr-xr-x 6 creativetechnologist staff 204 7 Sep 2011 lib 
drwxr-xr-x 6 creativetechnologist staff 204 11 Mar 2012 log 
drwxr-xr-x 3 creativetechnologist staff 102 30 Aug 2011 logs 
drwxr-xr-x 13 creativetechnologist staff 442 6 Feb 2012 public 
drwxr-xr-x 3 creativetechnologist staff 102 3 Feb 2011 script 
drwxr-xr-x 8 creativetechnologist staff 272 3 Feb 2011 test 
drwxr-xr-x 6 creativetechnologist staff 204 3 Feb 2011 tmp 
drwxr-xr-x 3 creativetechnologist staff 102 3 Feb 2011 vendor 
true 
=> nil 
1.9.2-p290 :002 > 

希望有幫助。

+0

奇怪的原因是它使用腳本/控制檯,但它不是腳本/服務器。也許因爲它是同一個實例? –

+0

我已經在端口3000和3001上啓動了兩個實例,一個用於客戶端,一個用於服務器。這種方式的代碼工程。但是這會在生產中起作用嗎? –

0

試試這個:

def save_page 
    `/usr/bin/curl -s http://127.0.0.1:3000/category_plist` # -s will silent curl's output except the page 
end 
+0

已經嘗試過,但沒有解決。輸出不寫入文件。正如我在上面的評論中所寫的,它與從同一實例調用服務器實例相關。 –