PHP running on PYTHON

Asked

Viewed 5,127 times

1

I’m trying to run a python script by php, but it doesn’t run python.

I used the code to test:

$cmdResult = shell_exec("ls & /usr/local/bin/python2.7 --version & echo done");

Returned:

done
LICENSE
example.py

When I type directly via shell:

[root@local folder]# /usr/local/bin/python2.7 --version
Python 2.7.6

Does anyone have any idea?

Additional info:

[root@local folder]# ls -all /usr/local/bin/py*
-rwxr-xr-x 1 root apache      84 Jul 21 21:53 /usr/local/bin/pydoc
lrwxrwxrwx 1 root root        24 Jul 21 21:43 /usr/local/bin/python -> /usr/local/bin/python2.7
-rwxrwxrwx 1 root apache 4669791 Jul 21 21:53 /usr/local/bin/python2.7
-rwxr-xr-x 1 root apache    1674 Jul 21 21:53 /usr/local/bin/python2.7-config

2 answers

2


Try the following on:

<?php

$comando = escapeshellcmd('./ficheiro_python.py');
$cmdResult = shell_exec($comando);
echo $cmdResult;

?>

As line "shebang" you should have the following in the python file:

#!/usr/bin/env python

You should also have permissions to run that file.

Successfully tested with Ubuntu 16, PHP7.0 and python 2.7.

  • $command = escapeshellcmd('/usr/local/bin/python2.7 --version'); $cmdResult = shell_exec($command); echo $cmdResult; Did not return the Python version.

  • @Hoppy intends to run a python script through PHP or run a shell script to know the python version through PHP?

  • Both do not return a result, I believe that if I can execute the command '/usr/local/bin/python2.7 --version' I can run python. I tried it the way you presented it. When I run on the shell ". /script.py or /usr/local/bin/python2.7 --version" Both work, but when PHP is running, it returns no result and does not run.

  • Try using the absolute directory and giving all permissions to the file ". py"

  • $cmdResult = shell_exec("/var/www/html/example.py"); echo $cmdResult; returned nothing. as I have 2 versions of python running, to run the file I put the line "shebang" pointing to the correct version: #!/usr/local/bin/python2.7

  • @Hoppy already found the problem. It is necessary to define the passage and not only the file as argument of the function "escapeshellcmd()". I have fixed the script.

  • i re-did the script in bash, I won’t have to worry about python anymore. x.x

Show 2 more comments

0

You should use two commercial E ("&&") instead of one:

$cmdResult = shell_exec("ls && /usr/local/bin/python2.7 --version && echo done");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.