Revision a0654819-79ae-44d9-8e63-5904347a01a2 - Stack Overflow

shell_exec — Execute command via shell and return the complete output as a string . [reference][1]


 <?php
 if(isset($_POST['switch'])){
 $c=escapeshellcmd("sudo python /home/pi/Desktop/test.py");
 $res=shell_exec($c);
 echo $res; // returns result to display
 }
 ?>

in your script,output is not printed that may seem to not working

 <?php
 if(isset($_POST['switch'])){
 $s=exec("sudo python /home/pi/Desktop/test.py");
 echo "$s"; 
 }
 ?>

EDIT:
>add full path of interpreter in the first line of python script . if you have installed more than one python version

 
>`$s=exec("sudo -u /home/pi/Desktop/test.py");` this gives permission to python file


> first of all make python file executable with `chmod +x /path/to/python-script.py`
 [1]: http://php.net/manual/en/function.shell-exec.php

AltStyle によって変換されたページ (->オリジナル) /