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);
Related
I am trying to compile python code using shell_exec(), or exec().
The code I am trying:
$output = shell_exec("C:/Users/AS/AppData/Local/Microsoft/WindowsApps/python.exe C:/wamp64/www/project/app/code.py 2>&1");
echo $output;
when I execute the command in shell or in the command line It works fine, However using php and (Apache sever) I get The system cannot execute the specified program. Though I can successfully execute different commands such as mkdir,
but if I try any command with .exe extension in my windows 11, I get the the same error as above.
I'm trying to run a shell script over ssh using php. I'm been able to achieve running a simple command but not a shell script and I've tried pretty much everything I could find over the google.
Here's the script:
<?php
use SSH2;
echo shell_exec("whoami");
?>
Same script passing the shell script won't run:
?php
use SSH2;
echo shell_exec("bash awsserver.sh");
?>
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
I have a python file that I would want to execute whenever a php page is called. The python file is in the same folder as the php file. The python script on execution edits a textfile that the php file accesses.
I have the following code:
<?php
exec("python somefile.py",$output);
$file = fopen("test.txt",'r');
....
For some reason, the python script never gets executed. I know this certainly as I can see it from the changes in the text file.
Since I was not sure if the script was made executable, so I included "python" on the command.
I also ran:
chmod +x somefile.py
just to make sure this was not the reason. But this did not help too.
What should I change to make the php page execute the python script whenever it is called?
This is most likely a permission issue.
Try
echo exec("whoami");
This will let you know who php is running as. Then you need to verify this user can run the python script. If the file was not created by the same daemon that runs python, you will most likely be denied permission.
Update
This will let you know who owns all the files you are working with. The file being written to needs to be writable by the user that is running python. If you are running python from ssh, that is most likely not the same user as when you run python from exec.
echo exec('whoami') . "<br>";
echo exec("ls -l test.txt") . "<br>";
echo exec("ls -l somefile.py") . "<br>";
Update 2
Because I constantly forget this exists.
passthru('python somefile.py 1 2>&1');
This will exec your python file, and output stderr to stdout.
I am trying to call a simple python script
#!/usr/local/python25/bin/python
print "hello world"
from the following php script
<?php
echo exec("/usr/local/python25/bin/python myfile.py");
?>
But nothing was happened.
Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)
Question Solved:
I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved.
Thank you so much for your help!
1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.
This is the reason you feel nothing was happened.
You can catch the whole output by this way.
exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);
Kind of an obvious point here, but can you run the python script from a terminal? Does it actually run?
Make sure the script is executable by whatever user PHP is running as - chmod 777 myfile.py, and just to be safe chmod 777 /usr/local/python25/bin/python. Also, make sure the python script is in the same directory as the PHP script, which is what your method of calling it requires.
Try changing your PHP script to this, and tell me what you see: (EDITED)
<?php
// Path to the python script - either FULL path or relative to PHP script
$pythonScript = 'myfile.py';
// Path to python executable - either FULL path or relative to PHP script
$pythonExec = '/usr/local/python25/bin/python';
// Check the file exists and PHP has permission to execute it
clearstatcache();
if (!file_exists($pythonExec)) {
exit("The python executable '$pythonExec' does not exist!");
}
if (!is_executable($pythonExec)) {
exit(("The python executable '$pythonExec' is not executable!"));
}
if (!file_exists($pythonScript)) {
exit("The python script file '$pythonScript' does not exist!");
}
// Execute it, and redirect STDERR to STDOUT so we can see error messages as well
exec("$pythonExec \"$pythonScript\" 2>&1", $output);
// Show the output of the script
print_r($output);
?>
If you want to capture the subprocess' stdout, you should use passthru
Also you don't need the first line of that python script if you're calling the python interpreter directly.