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
Related
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'm creating a webpage in PHP wherein if a button is pressed, the PHP script executes a python script using shell_exec() command. The python script creates a pdf file using fpdf python library. When I run the same from command shell, it works prefectly but when I try to run python script from PHP, it gives me nothing. Here is PHP code:
<!doctype html>
<?php
$command = escapeshellcmd('/home/amogh/server/test.py');
$output = shell_exec($command);
echo $output;
?>
Python script:
#! /usr/bin/python
#print ('hello')
from fpdf import FPDF
fp = open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read()
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')
Also, I'm using lighttpd server. Can anyone tell me where am I going wrong?
I'd guess the working directory is wrong and your script can't access the file it needs.
You may want to use
shell_exec("/home/amogh/server/test.py 2>&1");
in order to redirect stderr to stdout, so any error output from Python will be also in the $output variable, to help you debug things.
I've got my website hosted at one.com and I'd like to run a simple python script with php that returns 'hello' but I get no result.
It worked perfectly on local with Mamp.
What should I configure for it to work online ?
My php code:
<?php
$result = shell_exec('python test.py');
echo $result;
?>
The python script
print('hello')
First of all make sure that the python script is executable on the server. For safety reasons I would remove the space in the filename as well so make it just test.py for now. You can make it executable by simple changing the permissions using chmod +x test.py
Then, you can try the following lines of code in php
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
I'm trying to execute a python (Python 3) script from the PHP shell_execfunction but it doesn't seem to be working, I'm trying it on a windows system but it will be in CentOS 7 in production.
In the below code snippet nothing happens when the PHP runs. But it I use the $response = shell_exec("php -v"); line it displays the PHP version ok. I've also tried running the python script directly on the command line and it runs fine with no errors.
Code
$command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
Command Line
"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1
Update #1
I've gotten the python script to run via PHP, but the python script crashes once it hits driver = webdriver.PhantomJS(), the import exists in the python script from selenium import webdriver and all this functionality works fine once called directly via the command line but doesn't seem to work once the python script is called from PHP.
Here's a sample script that prints out test 1 but not test 2 when called from the PHP script.
from selenium import webdriver
print("test 1")
driver = webdriver.PhantomJS()
print("test 2")
driver.quit()
Update #2
Looks like it was an issue with paths, there is a file created in the python script that was using a relative path, once I changed the path to be a full path the script ran ok both in the command line and when being called via the PHP script.
You need to scape the command string.
Try using escapeshellarg function.
$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
http://php.net/manual/pt_BR/function.escapeshellarg.php
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.