Running python on the command line inside the controller - PHP / CodeIgniter

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

 public function deploy_test() { echo json_encode(system("python --version")); } 

When a user wants to deploy a test by clicking the test button on a web page, he will be able to perform such a task. However, when I click the test button, my output is:

 "" 

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

 public function deploy_test() { echo json_encode(system("ls -l")); } 

I get:

 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 

Can someone please help me deal with this?

+4
source share
5 answers

The problem is not with your code or PHP.

The problem is with your permissions.

php uses the permissions set in env-vars apache.

Which is perfect like:

 User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} 

in your apache2 / httpd conf file.

For instance:

Try to run:

 <?= `whoami` ?> 

through your shell and through your browser.

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

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

We should be close like:

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

To complete the operations, you will need to install the correct set of users and groups.

Seek: In a shell, what does "2 & 1" mean?

So your code should be:

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

Hope this helps!

+3
source

This works great for my production server.

 public function deploy_test() { echo json_encode(system("python --version 2>&1")); } 

with exit

 Python 2.7.3 "Python 2.7.3" 

The output of the unix command, printed twice as system (), displays the result in a browser. Therefore, exec () can be used instead of the system to avoid this.

 public function deploy_test() { echo json_encode(exec("python --version 2>&1")); } 

which outputs

 "Python 2.7.3" 

as was expected.

+1
source

I suspect this is not the way to go. Try:

Enter the full path to the python command (e.g. /usr/bin/python --version )

  • Find out with the command which is "python"

  • try to execute the script from the command line, php script.php '=> sometimes on the Internet the server sets up different things

  • make sure error display is enabled with

    ini_set ('display_errors', 1);

0
source
System

returns only the last row. system

 public function deploy_test() { system("python --version", $out); echo json_encode(implode($out)); } 
0
source

A bit of a hack, but you can find out the version number with 1 decimal, even if you are not allowed to execute python (which is the case with the "custom" CI).

 //find python path exec("which python", $path); //show all subdirs in python exec("ls -l ".$path[0]."*", $output); $output = implode("\n", $output); //preg match on version numbers preg_match_all("#python(\d+(?:\.\d{1,2})?)#", $output , $matches); $installed_versions = $matches[1]; //sort in reversing order $versions_sorted_desc = array_reverse($installed_versions); //latest version is element 0 $latest_version = $versions_sorted_desc[0]; echo $latest_version; 
0
source

All Articles