John Davidson

Run Python from PHP, status 0, not working

0 comments
Message:


I'm running simple code in PHP to execute Python code, i write in 2 file: python n php.


PHP code : (exec.php)


<?php 

$command = escapeshellcmd("/opt/lampp/htdocs/latihan/php_python/pi.py");
$output = exec($command, $out, $status);
print_r($output);
#print_r($out[0]);
echo "</br>";
print_r($status);
echo "</br>";
echo "</br>";

$command = shell_exec($command);
echo "<pre>".$command."</pre>";
echo "\n";

ob_start();
passthru('python /opt/lampp/htdocs/latihan/php_python/pi.py');
$output = ob_get_clean();

if( ini_get('safe_mode') ){
echo "safe mode is on";
}
else{
echo "it's not";
}
?>

Python Code : (pi.py)


#!/usr/bin/env python
print("Hello !");

Result 1 : (execute in terminal ):


Hello !</br>0</br></br><pre>Hello !
</pre>
it's not

Result 2 : (execute in browser ):


127

it's not

My reference :



My file permission :


-rw-r--r-- 1 www-data www-data 488 Nov 26 11:13 exec.php
-rwxr-xr-x 1 www-data www-data 41 Nov 26 10:18 pi.py

My question is, why i can't show result from python in browser ?


It get status 0, is it cause ? i Change permission to www-data root for pi.py still got same result(and change other option for both www-data and root or just one of them).


i try run "ls -l" it success shown in browser tough,,



My Answer:
php
$output = shell_exec('python script.py');
echo $output;
?>


Python code : (script.py)

python
print("Hello from Python")


Make sure that both files are in the same directory and have the correct permissions set. Also, check if the Python executable is in the system PATH. If the code is still not working, try adding the full path to the Python executable in the PHP code.

Rate this post

3 of 5 based on 6838 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us