无法在客户端执行来自php的python脚本

时间:2022-12-31 01:13:12

I am very new to PHP & Apache.

我是PHP和Apache的新手。

I am building a small webpage - which can run some python code on the server on click of a button from a client computer. To do this, I have read a few articles on the web which tell PHP is the way to do it. I also read that I wouldn't need AJAX. My understanding of PHP, AJAX and related technologies are at a beginner level.

我正在构建一个小网页 - 可以通过点击客户端计算机上的按钮在服务器上运行一些python代码。为此,我在网上阅读了一些文章,告诉PHP是如何做到这一点的。我还读到我不需要AJAX。我对PHP,AJAX和相关技术的理解处于初级阶段。

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

我在RHEL 6.5机器上运行了一个Apache服务器。我有PHP v5.3.3,Python v2.6。

PHP script (script.php) :

PHP脚本(script.php):

<?php
echo shell_exec("python pytest.py");
?>
  • Location : /var/www/html/script.php

    地点:/var/www/html/script.php

  • Permissions : chmod 777 script.php

    权限:chmod 777 script.php

Python Script (pytest.py) :

Python脚本(pytest.py):

import os
os.remove("/<path>/hon.txt")
  • Location : /var/www/html/pytest.py

    地点:/var/www/html/pytest.py

  • Permissions : chmod 777 pytest.py

    权限:chmod 777 pytest.py

Index HTML (index.html) :

索引HTML(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 :

完成的其他事情/测试:

  • The Python script works standalone, its a simple python script to delete a file.
  • Python脚本独立工作,它是一个删除文件的简单python脚本。

  • The PHP script also works standalone on the machine running the Apache server.
  • PHP脚本也可以在运行Apache服务器的计算机上独立运行。

  • When I try the button click from HTML, the PHP script does not seem to work.

    当我尝试从HTML单击按钮时,PHP脚本似乎不起作用。

  • 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 was not sure if this could be because of permissions, so I did a

    我不确定这是否是因为权限,所以我做了一个

    chmod 777 -R /var/www 
    

    Even then I had the same problems

    即便如此,我也有同样的问题

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

    我不确定哪里出错了,所以我对PHP脚本(script.php)做了两次更改:

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

    Over here - the second line works from browser, the first one does not. Which means my python portion is not getting executed.

    在这里 - 第二行是从浏览器工作,第一行不是。这意味着我的python部分没有被执行。

I read somewhere if this is a problem because my Apache server is not run with root privileges. Being new - I don't know how to check that.

如果这是一个问题我在某处读到,因为我的Apache服务器没有以root权限运行。新的 - 我不知道如何检查。

If any more information is needed - I can provide details in successive edits.

如果需要更多信息 - 我可以提供连续编辑的详细信息。

1 个解决方案

#1


3  

Try placing the full path to your file. In your case you just say 'pytest.py', so replace it with the full path.

尝试将完整路径放在文件中。在你的情况下你只是说'pytest.py',所以用完整的路径替换它。

Doing so, this would result in something like this:

这样做会导致类似这样的事情:

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

(Also, you missed one / in front of usr/bin/python)

(另外,你错过了一个/在usr / bin / python前面)

#1


3  

Try placing the full path to your file. In your case you just say 'pytest.py', so replace it with the full path.

尝试将完整路径放在文件中。在你的情况下你只是说'pytest.py',所以用完整的路径替换它。

Doing so, this would result in something like this:

这样做会导致类似这样的事情:

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

(Also, you missed one / in front of usr/bin/python)

(另外,你错过了一个/在usr / bin / python前面)