I am trying to execute a python script via PHP. For some reason it doesn't execute. I have included the first line on my python code below:
#!/usr/bin/env python
Here is my PHP code that is executing this code (this is in index.php. test.py is in the same folder as this.):
if(isset($_GET["on"])){
$command = escapeshellcmd("python test.py");
shell_exec($command);
echo "success";
}
But when I click the button to execute the script, I get "success" but the script doesn't execute. Any ideas why?
If you are on linux, your script might not have correct privileges: http://php.net/manual/en/function.shell-exec.php#37971
try:
chmod +x test.py
source: https://stackoverflow.com/a/19736494/6337641
Related
Trying a very simple task. Call python example.py from a php script from a windows xampp server.
Notes:
- Both files are in the same directory.
- Going to http://example.com/example.py works fine.
- Going throught the command line (cd inside where example.py is then (python example.py)) works also.
example.py
#! C:{path-to-python.exe}
print("Content-Type: text/html\n")
print("<html>")
print("<h1>Something.</h1>")
index.php
<?php
$command = escapeshellcmd('python example.py');
$output = shell_exec($command);
echo $output;
?>
Nother note:
- If I use $command = escapeshellcmd('example.py'); it opens the python script in my text editor.
Someone tell me plz what am I doing wrong. Thank you.
to call python script from php :
$result=exec("python test.py ");
if you want to passing parameters to python script :
$result=exec("python test.py param1 param2");
in python script if you want get parameters:
import sys
param1=sys.argv[1]
param2=sys.argv[2]
note : $result in php taking first print statement in python script
I want to execute python script in PHP but it does not work, the python script is not running. But when I directly execute the python script using cmd, the script work.
Below is my PHP code.
<?php
exec('python C:\wamp64\www\dashbot\mergeCSV.py',$output,$return);
if(!$return)
{
echo "success!";
}
else
{
echo "failed!";
}
?>
Try adding this to the first line of the python script
#!/usr/bin/env python
Make sure that the python script has the execution permission
(+x).
If you are running PHP via Apache, make sure the Apache
user(www-data) has permission to access & execute the script.
Mention the full path to python exec('full/path/to/python C:\wamp64\www\dashbot\mergeCSV.py',$output,$return);
I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.
First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result
I have a python script that uses ipyhon and it can be executed when I run it from the terminal using php -a (interactive shell), but when I type localserver/RDMIP_KE.php (test.php is the script wrtten with gedit in ubuntu) nothing happens and I only see a blank page. Any idea why?
<?php
$command = escapeshellcmd('/home/administrator/Desktop/RDMIP_KE.py');
$output = shell_exec($command);
echo $output;
?>
Just an update: I checked the log file and it gives me a list of errors for example about the pyplot: File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460,ax = gca() in plot
But again as I said before the python program works fine and can be executed with php if I run it from the php shell. I don't understand what's going on.
here is my code in control.php:-
<?php
$output = shell_exec("/var/www/script.py");
?>
why i cant run the shell_exec() command when i run the php script..? it should call the script.py and execute the py script.. but, when i run the control.php script..
note that i tried to run a normal program by using php where i tried to open gedit by using the command below:-
<?php exec('gedit');?>
the gedit is not executed..
anyone can help me?
Can you run "/var/www/script.py" in the BASH? if NOT, then check the script,make sure contain the following line:
#!/usr/bin/env python
then chmod +x script.py to make sure it is executable.
You probably need to specify python in shell_exec() explicitly:
$output = shell_exec("/usr/bin/python /var/www/script.py")
or
$output = shell_exec("python /var/www/script.py")
and you need to ensure that your script.py has a permission better than "-rwxrwxr-x" as your php probably is executed by a different user.
the reason is because of the php doesnt have the permission to write/read the py script in desktop.. so, just change the directory of the py to the same directory as the php file then eveything runs smoothly..