Running a Python script from PHP
Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.
In PHP use shell_exec function:
Execute command via shell and return the complete output as a string.
It returns the output from the executed command or NULL if an error
occurred or the command produces no output.
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
Into Python file test.py
, verify this text in first line: (see shebang explain):
#!/usr/bin/env python
If you have several versions of Python installed, /usr/bin/env will
ensure the interpreter used is the first one on your environment's
$PATH. The alternative would be to hardcode something like
#!/usr/bin/python; that's ok, but less flexible.In Unix, an executable file that's meant to be interpreted can indicate
what interpreter to use by having a #! at the start of the first line,
followed by the interpreter (and any flags it may need).If you're talking about other platforms, of course, this rule does not
apply (but that "shebang line" does no harm, and will help if you ever
copy that script to a platform with a Unix base, such as Linux,
Mac, etc).
This applies when you run it in Unix by making it executable
(chmod +x myscript.py) and then running it directly: ./myscript.py,
rather than just python myscript.py
To make executable a file on unix-type platforms:
chmod +x myscript.py
Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl)
and/or must be "executable". Also all commands into .py
file must have correct privileges.
Taken from php manual:
Just a quick reminder for those trying to use shell_exec on a
unix-type platform and can't seem to get it to work. PHP executes as
the web user on the system (generally www for Apache), so you need to
make sure that the web user has rights to whatever files or
directories that you are trying to use in the shell_exec command.
Other wise, it won't appear to be doing anything.
How to execute python script from php?
You can make an :
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
More information on this Stack Overflow Question.
Running a Python script from PHP
Run python script from php on webserver
Your hosting company might have disabled the shell-exec function for security reasons. I'm not saying it is the case, but you should check that first before even trying to run shell_exec.
Seealso:
PHP - How to know if server allows shell_exec
Plus, back in 2012, it wasn't possible with Altervista...
Running python script with php - working in console not in web browser
Problem solved!
As i mentioned in my question i was having problem with some kind of user privileges i modified my code and forced it to use bitnami as user and now its working!
<?php
header('Access-Control-Allow-Origin: *');
define('ROOTPATH', dirname(__FILE__));
$output = shell_exec('sudo -H -u bitnami python test.py ');
echo $output;
?>
Related Topics
Php: Check Who Had Read Sent Email
Get the Price of an Item on Steam Community Market with PHP and Regex
Using If(!Empty) with Multiple Variables Not in an Array
How to Trust PHP _Destruct() Method to Be Called
PHP - Listing All Directories and Sub-Directories Recursively in Drop Down Menu
How to Add Query Parameters in the Zf2/Zf3 Url View Helper
How to Make 5 Random Numbers with Sum of 100
Escaping Column Names in Pdo Statements
Apple Sign in "Invalid_Client", Signing Jwt for Authentication Using PHP and Openssl
PHP 5.4 Pdo Could Not Connect to MySQL 4.1+ Using the Old Insecure Authentication
Creating an Image Without Storing It as a Local File
Prevent Direct Access to a PHP Page
Does PHP Optimize Tail Recursion
Make Text Wrap in a Cell with Fpdf
A PHP/Pthreads Thread Class Can't Use Array