我需要从我的 php 脚本中使用命令行参数运行 python 程序
这是我的代码
<?php
$output=shell_exec('python /path/to/python_from_php.py input1 input2');
echo $output;
?>
我正在将 shell_exec 函数中的内容作为输出。 我试过只使用
$output=exec('python /path/to/python_from_php.py input1 input2');
我只得到第二个输入作为我的输出。 但我想要的是我应该能够通过我的 python 脚本打印输入内容。如果我删除“echo $output”行,我不会得到任何输出。 我的python代码
import sys
a=(sys.argv)
for i in (sys.argv):
print i
最佳答案
此代码为您提供 shell 的输出
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
来自这个引用 php 还要确保 php 没有在安全模式下运行 但你可以尝试从 php 调用 python 脚本
<?php
$my_command = escapeshellcmd('python path/to/python_file.py');
$command_output = shell_exec($my_command);
echo $command_output;
?>
关于php - 从 php 运行 shell 命令,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099466/