1
我知道在StackOverflow上有類似的問題,但我已經嘗試了所有這些方法,但都沒有工作。在我的筆記本電腦上,我有一臺Apache服務器,一個使用PHP和Python腳本構建的網站。我如何在php中嵌入python腳本?
嘗試:
1)system
$mystring = system('python myscript.py myargs', $retval);
2)和3)和JSON $溫度= EXEC($命令,$輸出);
PHP:
#first case
command= 'C:\wamp\www\com\non.py file';
$temp = exec($command, $output);
echo(" output ");
echo $output;
echo " hello ";
echo $temp;
#second case
// Execute the python script with the JSON data
$result = shell_exec('python /confronto/non.py ');
#. escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
蟒蛇:
import sys, json
def tests():
b = 2
print("inside test")
print(b)
a = 4
return a
#if __name__ == "__main__":
c = tests()
print("main")
print(c)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
4)Apache的配置設置好的:
AddHandler cgi-script .cgi .py