Can not execute python script from php, client side

I am very new to PHP and Apache.

I am creating a small web page that can run some python code on the server when a button is clicked from the client computer. To do this, I read several articles on the internet that say PHP is the way to do it. I also read that I don't need AJAX. My understanding of PHP, AJAX and related technologies is at a basic level.

I have Apache server running on RHEL 6.5 server. I have PHP v5.3.3, Python v2.6.

PHP script (script.php):

<?php
echo shell_exec("python pytest.py");
?>

      

  • Location: /var/www/html/script.php

  • Permissions: chmod 777 script.php

Python script (pytest.py):

import os
os.remove("/<path>/hon.txt")

      

  • Location: /var/www/html/pytest.py

  • Permissions: chmod 777 pytest.py

HTML Index (index.html):

<!DOCTYPE html>
<html>

<header>
<title>Python Run</title></header>
<body>

<p>
<b><font size="10" color="red"><div align="center">Run Python Jobs</div></font></b>
</p>

<table style="width:100%">
  <tr>
    <td>Script1</td>
    <td>Description</td>
    <td>
                <form action="script.php" method="get">
                        <input type="submit" value="Run Py Script">
                </form>
    </td>
  </tr>
</table>
</body>
</html>

      

Other things / tests done:

  • Python script works standalone, its a simple python script to delete a file.
  • The PHP script also runs autonomously on the machine running the Apache server.
  • When I try to click a mouse button with HTML, the PHP script doesn't seem to work.

  • I have checked the log messages from

    cd /var/log/
    tail messages -f
    
    May  6 18:22:01 machine abrt: detected unhandled Python exception in 'pytest.py'
    
          

  • I wasn't sure if this was possible because of the permissions, so I did

    chmod 777 -R /var/www 
    
          

    Even then I had the same problems

  • I wasn't sure where things were going wrong, so I made 2 more changes to the PHP script (script.php):

    <?php
    echo shell_exec("/usr/bin/python /<path>/pytest.py");
    echo shell_exec("ls");
    ?>
    
          

    Here - the second line works from the browser, the first one does not. This means my python part is not executing.

I read somewhere if this is a problem because my Apache server does not start with root privileges. Being new - I don't know how to test it.

If you need more information, I can provide details on subsequent changes.

+3


source to share


1 answer


Try putting the full path to your file. In your case, you are just saying 'pytest.py'

, so replace it with the full path.

So it will result in something like this:



<?php
    echo shell_exec("/usr/bin/python /home/username/scripts/pytest.py");
?>

      

(Also, you missed one /

before usr/bin/python

)

+3


source







All Articles