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";

?>
peekaboo
  • 105
  • 1
  • 15

2 Answers2

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";

?>
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • i tried replacing with this $command = escapeshellcmd("/usr/bin/python z_admin_face/testPyscript.py"); – peekaboo Sep 02 '15 at 22:45
  • 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 – peekaboo Sep 02 '15 at 22:45
  • 1
    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. – Shawn Mehan Sep 02 '15 at 22:47
  • its in /usr/bin/python and yes both the py and php files are in the same directory , it still doesn't work. Weird ;( – peekaboo Sep 02 '15 at 22:53
  • ugh. you have problems that have nothing to do with your code. what happens if you replace $command with `$command = escapeshellcmd("which python");` – Shawn Mehan Sep 02 '15 at 22:59
  • and what happens if you run the py_run.php from the command line in the same dir as the file, e.g. `$ /usr/bin/php py_run.php`. – Shawn Mehan Sep 02 '15 at 23:02
  • hmm from your point it seems like im executing or calling which pyton file to run and am pointing directly at the python file directory to run in the end? – peekaboo Sep 02 '15 at 23:07
  • err, no. I literally want you to replace the escapeshellcmd payload with "which python" which should return output along the lines of `/usr/bin/python`. I also want you to run the php file as I indicate from the command line to see if you can run the php at all, and whether it then works with the call to the shell and returns the normal output. There are two (2) separate tests here, so please answer them independently. – Shawn Mehan Sep 02 '15 at 23:10
  • /usr/bin/python php_face2 get display on my browser webpage but nothing get printed or run from my terminal window ...this is when replacing $command = escapeshellcmd("which python"); – peekaboo Sep 02 '15 at 23:11
  • oh wait its a script, so ya it seems that it works so its prolly a problem with my python file? – peekaboo Sep 02 '15 at 23:12
  • $ /usr/bin/php py_run.php `command not found` .but i believe it wont work for this test as its a php file? and cant run from the terminal window, there isnt a she bang on top of my php file either so no interpreter will be called to intepret the php file anyway. i believe – peekaboo Sep 02 '15 at 23:18
  • at the command line, in the same directory as the py_run.php file, type the following at the command prompt and report the return, please: `/usr/bin/php py_run.php` If it doesn't work, please paste what you get typing `ls -al` in same said directory – Shawn Mehan Sep 02 '15 at 23:20
  • i forgot to mention i did not move the php file to /usr/bin/php as i cant see that folder, so i manually called the directory via cd command and access its full path – peekaboo Sep 02 '15 at 23:22
  • the return for that would be `is a directory` – peekaboo Sep 02 '15 at 23:23
  • Please copy the command and output from your shell, like: shawnmehan$ ls -al total 40 drwxr-xr-x 8 shawnmehan staff 272 Sep 2 15:57 . drwxr-xr-x 3 shawnmehan staff 102 Aug 24 20:35 .. drwxr-xr-x 4 shawnmehan staff 136 Sep 2 15:32 .phpintel drwxr-xr-x 3 shawnmehan staff 102 Aug 24 20:44 bin -rw-r--r-- 1 shawnmehan staff 149 Sep 2 15:58 duh.php -rw-r--r-- 1 shawnmehan staff 21 Sep 2 15:57 pyrun.py – Shawn Mehan Sep 02 '15 at 23:25
  • ` total 48 drwxr-xr-x 7 jian admin 238 Sep 3 08:49 . drwxr-xr-x 12 jian admin 408 Sep 2 14:59 .. -rw-r--r--@ 1 jian admin 6148 Sep 2 16:28 .DS_Store -rw-r--r-- 1 jian admin 352 Sep 2 17:42 face_admin.php -rw-r--r-- 1 jian admin 149 Sep 3 09:10 face_admin_run.php -rw-r--r-- 1 jian admin 1323 Sep 3 08:49 face_admin_run_backup.php -rwxr-xr-x 1 jian admin 57 Sep 2 13:50 testPyscript.py ' face_admin_run.php is the file with the php code to run the py script and testPyscript.py is the python file – peekaboo Sep 02 '15 at 23:26
  • good. now do the same with `/usr/bin/php face_admin_run.php` like `$ /usr/bin/php face_admin_run.php /usr/bin/python` . Here, the last string is the output of my `which python` in the php file. – Shawn Mehan Sep 02 '15 at 23:34
  • err dont quite get the last instruction, do you want me to run the face_admin_run.php in its directory followed by the directory of python?' – peekaboo Sep 02 '15 at 23:40
  • jians-MacBook-Pro:~ jian$ cd /usr/bin/python -bash: cd: /usr/bin/python: Not a directory jians-MacBook-Pro:~ jian$ /usr/bin/python Python 2.7.10 (default, Aug 7 2015, 20:58:43) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> – peekaboo Sep 02 '15 at 23:42
  • in the directory with your php file, type: `/usr/bin/php face_admin_run.php`. Copy and paste that and the return, please. – Shawn Mehan Sep 02 '15 at 23:43
  • jians-MacBook-Pro:fyp_4_sem2 jian$ cd z_admin_face jians-MacBook-Pro:z_admin_face jian$ /usr/bin/php face_admin_run.php /usr/bin/python: can't open file 'z_admin_face/testPyscript.py': [Errno 2] No such file or directory php_face2jians-MacBook-Pro:z_admin_face jian$ base on my current php file `` – peekaboo Sep 02 '15 at 23:53
  • OK. So your php file is executing. Now it is complaining that it can't find `testPyscript.py`. And that is because you have told it that it is in yet another subdirectory called `z_admin_face`. Go to your php file and remove the `z_admin_face/testPyscript.py` and replace it with only `testPyscript.py`. Rerun your php from the command line and report. – Shawn Mehan Sep 02 '15 at 23:59
  • jians-MacBook-Pro:z_admin_face jian$ /usr/bin/php face_admin_run.php hello world python script php_face2jians-MacBook-Pro:z_admin_face jian$ – peekaboo Sep 03 '15 at 00:06
  • it works now but i just wanna clarify something when i execute my script, when i press the button which runs the php file, the script execute and output hello world in the browser console, it doesnt and wont show the same command in terminal window right (what im asking is this the right behaviour? or will it show a command in the terminal window as well?) – peekaboo Sep 03 '15 at 00:08
  • now it works from the escapeshellcmd. Is it working from the web page? – Shawn Mehan Sep 03 '15 at 00:08
  • yes it displays on my browser console now too. Thank you for your help. – peekaboo Sep 03 '15 at 00:10
  • whew. please mark this answer as complete. is there a badge for patience :) – Shawn Mehan Sep 03 '15 at 00:14
  • hahha thanks alot for guiding me through, yes i did upadte my question and also thanked you :) haha yes you deserve a badge for patience . – peekaboo Sep 03 '15 at 00:15
  • thank you it works now. it should upvote more. – kta Mar 24 '21 at 21:30
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");
Shelvacu
  • 4,245
  • 25
  • 44
  • no harm in being vigilant. The escapeshellcmd isn't hurting anything, so why not leave it? – Shawn Mehan Sep 02 '15 at 22:39
  • 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 – Shelvacu Sep 02 '15 at 22:40
  • @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` – Shelvacu Sep 02 '15 at 23:09