2015-04-17 141 views
1

我有一個程序從python調用PHP,使用popen,並且我沒有從我的調用中獲取任何stdout或stderr。我爲什麼不能從PHP獲得輸出,使用Python Popen

有問題的線路有:

task = subprocess.Popen(
     ['/usr/bin/php', exec_path], 
     stdout=subprocess.PIPE, 
     stderr=subprocess.PIPE, 
     env=env 
    ) 
stdout_str, stderr_str = task.communicate() 
task_result = task.returncode 

當我更換LS PHP的路徑,它的工作原理:

task = subprocess.Popen(
    ['/bin/ls', exec_path], 
    stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE, 
    env=env 
) 

stdout_str, stderr_str = task.communicate() 
task_result = task.returncode 
# Works as expected 

當我從shell運行PHP命令,但它產生的輸出。

作爲參考,PHP文件是:

<? 
echo "YAY"; 
?>% 
+0

'exec_path'在兩種情況下都是一樣的吧?我想知道這是否是一個許可問題。 –

+0

但我仍然能夠從命令行執行該文件,如php /tmp/regraphd_testing_dir_A5wdrS/queries/tmp0AKqI6.php, 但是,路徑是相同的 – riri

+0

不應該PHP文件以'<?開頭。 php'? –

回答

0

我固定我的問題。之前我寫過文件內容,並且忘記flush()它,所以當exec調用發生時文件是空的。

相關問題