在控制器内的命令行中执行python - PHP / CodeIgniter [英] executing python in the command line inside a controller - PHP/CodeIgniter

查看:221
本文介绍了在控制器内的命令行中执行python - PHP / CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 automation.php 控制器中,我有以下函数:

  public function deploy_test(){

echo json_encode(system(python --version));
}

当用户要部署测试时,通过单击 test 按钮,他将能够完成这样的任务。
然而,当我点击 test 按钮时,我的输出是:

 

同时,当我用命令执行相同的函数:

  public function deploy_test(){

echo json_encode(system(ls -l));
}

我得到:

 共32 
drwxr-xr-x。 15 philippe philippe 4096 Mar 4 16:48 application
drwxrwxr-x。 2 philippe philippe 4096 Mar 4 17:28 css
-rw-r - r--。 1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x。 2 philippe philippe 4096 Feb 27 15:38 js
-rw-r - r--。 1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x。 8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x。 12 philippe philippe 4096 1月30日11:53 user_guide

有人可以帮我弄清楚吗?

解决方案

问题是您的代码或PHP



问题在于您的权限。



php使用在apache的env-vars中设置的权限。



其理想地设置为:

 用户$ {APACHE_RUN_USER} 
组$ { APACHE_RUN_GROUP}

在您的apache2 / httpd配置文件下。


$ b b

例如:



尝试执行:

  ;?=`whoami`?> 





你的浏览器可能会说www-data和shell会说你的用户名或者如果你在AWS - 默认,你会得到 root



您不必使用 system(),而改用 exec() / p>

我们应该如下:

  echo json_encode python --version)); 

执行操作需要您设置正确的用户和群组。



查找:在bash shell中,什么是, 2& 1?



因此您的代码应为:

  echo json_encode(exec(python --version 2>& 1)); 

希望它有帮助!


Inside my automation.php controller, I have the following function:

public function deploy_test() {

      echo json_encode(system("python --version"));
 }

When the user wants to deploy a test, by clicking a test button in the webpage, he would be able to accomplish such a task. However, when I click the test button, my output is:

""

Meanwhile, when I execute the same function with the command:

public function deploy_test() {

    echo json_encode(system("ls -l"));
}

I'm getting:

total 32
drwxr-xr-x. 15 philippe philippe 4096 Mar  4 16:48 application
drwxrwxr-x.  2 philippe philippe 4096 Mar  4 17:28 css
-rw-r--r--.  1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x.  2 philippe philippe 4096 Feb 27 15:38 js
-rw-r--r--.  1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x.  8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x. 12 philippe philippe 4096 Jan 30 11:53 user_guide

Could someone please help me to get that straighten out?

解决方案

The problem is not with your code or PHP.

The problem is with your permissions.

php uses permissions which are set in the env-vars of apache.

Which is ideally set as :

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

under your apache2 / httpd conf file.

For example:

Try running:

<?= `whoami` ?>

via your shell and via your browser.

Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root

You do not have to use system() , use exec() instead.

We should be close as :

echo json_encode(exec("python --version"));

Performing operations will require you to have correct User and Groups set.

Look up for : In the bash shell, what is " 2>&1 "?

So your code should be :

echo json_encode(exec("python --version 2>&1"));

Hope it helps!

这篇关于在控制器内的命令行中执行python - PHP / CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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