<p>在Ubuntu服务器10.04上测试。我希望它也能在Arch Linux上帮助您。</p>
<p>在PHP中<a href="https://www.php.net/manual/en/function.shell-exec.php" rel="noreferrer">use shell_exec function</a>:</p>
<blockquote>
<p>Execute command via shell and return the complete output as a string.</p>
<p>It returns the output from the executed command or NULL if an error
occurred or the command produces no output.</p>
</blockquote>
<pre><code><?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
</code></pre>
<p>在Python文件<code>test.py</code>中,验证第一行中的文本:<a href="http://www.stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script/2429517">(see shebang explain)</a>:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>还有Python文件<a href="http://www.php.net/manual/en/function.shell-exec.php#37971" rel="noreferrer">must have correct privileges</a>(如果PHP脚本在浏览器或curl中运行,则为用户www data/apache执行)
和/或必须是“可执行的”。此外,<code>.py</code>文件中的所有命令都必须具有正确的权限:</p>
<p>摄于<a href="http://www.php.net/manual/en/function.shell-exec.php#37971" rel="noreferrer">from php manual</a>:</p>
<blockquote>
<p>Just a quick reminder for those trying to use shell_exec on a
unix-type platform and can't seem to get it to work. PHP executes as
the web user on the system (generally www for Apache), so you need to
make sure that the web user has rights to whatever files or
directories that you are trying to use in the shell_exec command.
Other wise, it won't appear to be doing anything.</p>
</blockquote>
<p>到<a href="http://nersp.nerdc.ufl.edu/~dicke3/nerspcs/chmod.html" rel="noreferrer">make executable a file on unix-type platforms</a>:</p>
<pre><code>chmod +x myscript.py
</code></pre>