簡體   English   中英

無法從PHP shell_exec執行Python腳本

[英]Unable to execute Python script from PHP shell_exec

我正在嘗試從PHP shell_exec函數執行python(Python 3)腳本,但是它似乎沒有用,我正在Windows系統上嘗試它,但是它將在生產中的CentOS 7中使用。

在下面的代碼片段中,PHP運行時沒有任何反應。 但是我使用$response = shell_exec("php -v"); 這行顯示的是PHP版本。 我也嘗試過直接在命令行上運行python腳本,它運行正常且沒有錯誤。

    $command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
    $response = shell_exec($command);
    //$response = shell_exec("php -v");
    echo $response;

命令行

"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1

更新#1

我已經通過python運行了python腳本,但是一旦它到達driver = webdriver.PhantomJS() ,python腳本就會崩潰, from selenium import webdriver的python腳本中存在from selenium import webdriver並且一旦通過命令行,但是一旦從PHP調用python腳本,似乎就無法使用。

這是一個示例腳本,當從PHP腳本調用時,該腳本打印出test 1但不輸出test 2

from selenium import webdriver

print("test 1")

driver = webdriver.PhantomJS()

print("test 2")

driver.quit()

更新#2

看起來這是路徑問題,在python腳本中創建了一個使用相對路徑的文件,一旦我將路徑更改為完整路徑,腳本就可以在命令行中以及在通過PHP腳本。

您需要轉義命令字符串。

嘗試使用escapeshellarg函數。

$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;

http://php.net/manual/pt_BR/function.escapeshellarg.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM