I have a python program that i want to execute from php I already tried "exec,escapeshellcmd,shell_exec,passthru,popen" but no one is giving result.
i already execute "hello word" program successfully but the program that i needed is not executing and not displaying any error.
echo $python = exec("python python/interactive.py");//this file not working
echo $python = passthru("python python/python.py ");// this is working with hello world.
$output = shell_exec($command);
echo $output;
I suggest that you look into a python web framework like Flask or Django which are the two main ones if you want to be executing python scripts on the web.This would be the best way to do it.
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 have a python script which takes data from a csv file via pandas. When I run this solely this works fine. But when I run it within php it does not work. I want it to work like that so I can output the python output onto the website.
Python code:
#!/usr/bin/env python
import pandas as pd
teams = pd.read_csv("/var/www/html/teams.csv")
print(str(teams['punt'][0]))
PHP code:
<?php
$command = escapeshellcmd('python3 /var/www/html/python.py');
$output = shell_exec($command);
echo $output;
?>
Under the PHP is some irrelevant html code. It would be very helpful if someone could help me!
EDIT:
When I run the php in terminal it does not give any errors and it gives the result perfectly. But when I run localhost it does not work but I cannot seem to find any errors. I also tried running a different python script which only prints "hello" and when I try that the code does work and the text shows up in the browser.
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 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 this Ruby script (test.rb):
print "hello"
And I have this PHP script (test.php):
$cmd = "ruby test.rb";
system($cmd);
Now I call my PHP script from CLI this way:
php test.php
And I get no output (it should print "hello")
Why?
system would capture the output of the ruby script.
you might want to do:
$cmd = "ruby test.rb";
echo system($cmd);
Its working for me, check whether both ruby script and php in the same folder and installed ruby in your machine
<?php
$cmd = "ruby test.rb";
system($cmd);
?>