2017-06-29 185 views
0

我嘗試了許多解決方案,但沒有它的工作原理:運行Python文件Linux服務器上

echo '<pre>'; 

shell_exec("python /home/folder/python/mapfile_compress.py"); 
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid"); 
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py"); 
system("bash /home/folder/python/mapfile_compress.py"); 
passthru("bash /home/folder/python/mapfile_compress.py"); 
passthru("/home/folder/python/mapfile_compress.py"); 
exec("bash /home/folder/python/mapfile_compress.py"); 

echo '</pre>'; 

我indivdually推出他們,但在任何情況下,螢火蟲返回:'<pre>'

所以,我想這個代碼創立堆棧溢出:

$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py'); echo $command; $output = shell_exec($command); echo $output;

但螢火蟲返回任何內容。 我的Python文件開頭#!/usr/bin/env python,如果我在服務器上啓動它!

你知道如何從PHP文件啓動我的python文件?

回答

0

在我而言這是工作,如果我寫這樣的代碼:

$command = escapeshellcmd('python /path/to/your/file.py'); 
exec($command); 
0

起初,您是否已經在php.ini中啓用了shell_exec/system/passthru命令?

shell_exec("python /home/folder/python/mapfile_compress.py"); 

我想,這可能是您的$ PATH問題。嘗試是這樣的:(使用完整路徑蟒蛇)

shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py"); 
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py"); 
1

chmod在出錯成功返回0和> 0。

確保該文件能夠通過以Web用戶身份執行而運行。當正確設置+ x時,可以通過調用$ /path/to/your/file.py來執行它,腳本#!/usr/bin/env python中第一行的shebang應該根據env定義正確的python。

您可以通過運行測試:

$ /usr/bin/env python /path/to/your/file.py 

所以,檢查你的文件權限檢查,如果該文件是由運行PHP腳本的用戶執行。

只是爲了測試,你可以只打印了幾行你的Python文件

#!/usr/bin/env python 

print "test line 1" 
print "test line 2" 

然後,如果你已經驗證權限和正確使用Python,你可以在你的php做到這一點。

$command = escapeshellcmd('/path/to/your/file.py'); 
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line. 
echo "<pre>{$output}</pre>; 
+0

好了,我嘗試從我的PHP文件使用chmod與此代碼:'搭配chmod(「/家/文件夾/蟒蛇/test.py「);'但它是一樣的東西螢火蟲沒有返回任何東西 –

+0

你必須使用chmod從PHP?在執行它之前,您嘗試執行的文件應該在服務器上可用並可執行。 – Robert

+0

是的,我從PHP文件啓動它。我只能從PHP文件啓動它,但在我的PC上,我的python文件:test.py工程 –

相關問題