使用shell_exec()在php脚本中执行python [英] Execute python in a php script using shell_exec()

查看:147
本文介绍了使用shell_exec()在php脚本中执行python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在php服务器(LAMP)中执行python时遇到了一个奇怪的问题. (关闭安全模式)

I'm experiencing a weird problem in trying to execute python in a php server (LAMP). (safe_mode off)

如果我输入:

$output = shell_exec("ls -lah");
echo "<pre>$Output</pre>";

我得到了ls命令的结果.与$output = shell_exec("tar --version");和其他应用程序(例如gzip)相同.

I got the result of the ls command. Same for$output = shell_exec("tar --version"); and other applications, such as gzip.

但是,如果我切换到以下任何一行:

However, if I switch for any of these lines:

$output = shell_exec("python --version");
$output = shell_exec("python2.7 --version");
$output = shell_exec("/usr/bin/python --version");
$output = shell_exec("python my_script.py");

和其他类似的变体,我没有结果.该命令未执行,没有执行python bitecode,echo保持沉默.

And other variants of this kind, I get no results. The command is not being executed, the python bitecode not made and the echo remains silent.

我也尝试了exec()命令,但没有成功.

I have also tried with the exec() command with no more success.

推荐答案

我认为这可能会有所帮助...

I think this may help...

看起来需要正确路由python调用的输出. 我能够在index.php文件中完成此工作,以返回python版本...

looks like the output for the python call needs to be routed properly. I was able to make this work within my index.php file for returning the python version...

shell_exec("python -V 2>&1");

Here is where I found the answer.

这篇关于使用shell_exec()在php脚本中执行python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆