PHP running PYTHON

1

I'm trying to run a python script through php, but it does not 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 ideas?

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
    
asked by anonymous 25.07.2016 / 21:39

2 answers

1

Try the following:

<?php

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

?>

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

#!/usr/bin/env python

You should also have permissions to run this file.

Tested successfully with Ubuntu 16, PHP7.0 and python 2.7.

    
25.07.2016 / 22:32
0

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

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