Running a python script with php on a web server - php

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;
?>

Related

Call python script with php (windows xampp server)

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

php can run the python programm from php shell but not on the webbrowser

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.

Execute python in a php script using shell_exec()

I'm experiencing a weird problem in trying to execute python in a php server (LAMP). (safe_mode off)
if I type:
$output = shell_exec("ls -lah");
echo "<pre>$Output</pre>";
I got the result of the ls command. Same for$output = shell_exec("tar --version"); and other applications, such as gzip.
However, if I switch for any of these lines:
$output = shell_exec("python --version");
$output = shell_exec("python2.7 --version");
$output = shell_exec("/usr/bin/python --version");
$output = shell_exec("python my_script.py");
And other variants of this kind, I get no results. The command is not being executed, the python bitecode not made and the echo remains silent.
I have also tried with the exec() command with no more success.
I think this may help...
looks like the output for the python call needs to be routed properly.
I was able to make this work within my index.php file for returning the python version...
shell_exec("python -V 2>&1");
Here is where I found the answer.
If you are trying to run the python script using the following code
$output = shell_exec("python my_script.py");
you will need to use absolute path for my_script.py and give all permissions (I am not sure which ones are sufficient) for the python file.
I think you need to refer to the full path for your python.
for example use this instead:
$output = shell_exec("/usr/bin/python full_path/my_script.py")
instead of:
$output = shell_exec("python my_script.py");
I think kernel not able to find the path for python where it is installed..if you can do echo $PATH..it will show all the paths where to be search a command if given
add your python part there and then it may work or you can give absolute path(other than /usr/bin/) see if it works..I need to test it too.
What does
which python
tell you, both from the command line and from shell_exec()? It should tell you which (if any) Python interpreter it's finding (from $PATH). Don't forget that it's quite possible that the $PATH used from the Linux command line might not be the same as the $PATH used by shell_exec()! Once you find the Python interpreter you want to use, you might have to hard code it in the shell_exec().
Most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the 'sudo visudo' command and add the following line to the sudoers file:
www-data ALL=NOPASSWD: ALL
Also, make sure that the /var/www directory belongs to the www-data user and group (use sudo chown -R www-data:www-data /var/www to set the correct owner). The details are here http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi
Also refer
Can't execute python script from php

Executing Ruby script from PHP and getting output

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);
?>

php run shell script

I'm trying to run a shell script from a php frontend
heres the shell file (run.sh chmod to 777)
#!/bin/bash
wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
Heres the php front end
<?php
$output = shell_exec("run.sh");
echo "<pre>$output</pre>";
?>
But it php page doesn't return anything except
<pre></pre>
Have you tried doing error_reporting(-1); at the top of your PHP script. Likely shell_exec is disabled on your server.
Would you try
$output = shell_exec("sh run.sh");
Check what user the php/web server is actually run as - e.g. "www-user" may have no permissions whatsoever to do the things your script is trying to do (and for good reason).

Categories