2

Would like to ask , as I am designing a webpage whereby there will be a few buttons that when press will run different python scripts.

For now I have tried a basic example by using both jquery and php to pass the command to run just a single python script first that prints out hello world in the terminal console but it isn't working. I have tried searching but could not seem to find the solution. Any help? (One of them was about using fast-cgi? anyone can shed some light on this?)

Note: -I am coding on Mac OS X and using XAMPP but will be porting this code to raspberry pi with LAMPP installation. (Hence command line and advice if differ for the two OSes will be greatly appreciated)

My code:

Below are the codes where by i create a button when on press it runs a simple python script

Main.php

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="js/general_v2.js"></script>
</head>

    <body>

        <button id="button_py1" type="button">Press to test Py</button>

 </body>
 </html>

below is the code from general_v2.js

$(document).ready(function(){
  $('#button_py1').click(function() {
  e.preventDefault();

  $.ajax({
   type: "POST",
   url: "z_py/py_run.php",


   success: function(output_string){
    console.log("js_success");

   },
   error:function(err){
    //handle error
    alert('Error submitting form' + err);
   }

});    
});
});

below is the code for py_run.php

<?php
        $command = escapeshellcmd("python z_py/testPyscript.py");
        $output = shell_exec($command);
        echo $output;
        echo  "php_works";

?>

the python file:

#! /usr/bin/env python

print "hello world python script"

Edit: -I have tried executing the script directly by keying in the url path name it does't work either(it does print to echo out a random test message so it is not permission error on the file i believe.) -Regarding the php code on executing python, im not familiar with using it(i found that on the forumn(can't remember where but i tried and doesnt work), found out about exec command as well and tried it as shown below and it doesn't work either :

This is my alternative Attempt on using exec (py_run.php)

<?php
        $command = "python z_py/testPyscript.py";
        exec($command);

?>

SOLVED! thanks to @Shawn Mehan

The solution would be that the python file is already in the same directory as the php file, hence removing the path name solves the issue.

updated code:

<?php
        $command = escapeshellcmd("/usr/bin/python testPyscript.py");
        $output = shell_exec($command);
        echo $output;
        echo  "php_works";

?>

2 Answers 2

1

Well, your code works for me if I change your py_run.php slightly to include the path to python, e.g.:

<?php
        $command = escapeshellcmd("/path/to/python/python z_admin_face/testPyscript.py");
        $output = shell_exec($command);
        echo $output;
        echo  "php_face2";

?>
Sign up to request clarification or add additional context in comments.

28 Comments

i tried replacing with this $command = escapeshellcmd("/usr/bin/python z_admin_face/testPyscript.py");
is this the right path for mac os x ? "/usr/bin/python" Cause when i put the full path nothing gets printed in terminal window
try which python in the cmd line to see where your python lives. and, as @Shel wrote, you need to make certain that you have enough path to get to testPyscript.py. To make it easy if you continue having problems, put the pyscript.py in the same dir as the py_run.php to remove any path problems.
its in /usr/bin/python and yes both the py and php files are in the same directory , it still doesn't work. Weird ;(
ugh. you have problems that have nothing to do with your code. what happens if you replace $command with $command = escapeshellcmd("which python");
|
0

You don't want to use escapeshellcmd here. That function is meant for escaping arguments so that a user can't enter foo;reboot and make your system reboot. However, since you don't have any user input, you don't need it.

Instead you want:

$output = shell_exec("python z_admin_face/testPyscript.py");

3 Comments

no harm in being vigilant. The escapeshellcmd isn't hurting anything, so why not leave it?
try including the full path to the python binary like @Shawn suggested, as well as the full path to the python script. I suspect you are already in the 'z_py' directory when the php script is run
@ShawnMehan It escapes it to python\ z_py/whatever which means it will try to execute the file named python z_py/whatever instead of the file named python with the arguments z_py/whatever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.