How to fix no output with python linked to php? - php

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.

Related

Calling a Python script from PHP does not work

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.

Execute python program using php on server

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.

Not getting output from python script when run through PHP

I have a python script that prints out the program names that are currently in the volume mixer in Windows 10.
This works fine when I run it in the cmd.
C:\wamp\www\Volume>py test.py
firefox.exe,Spotify.exe,Microsoft.Photos.exe,Steam.exe,
and here is my python script.
import sys
from pycaw.pycaw import AudioUtilities
def main():
list = ''
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session.SimpleAudioVolume
if session.Process and session.Process.name():
list += session.Process.name() + ','
sys.stdout.write(list)
if __name__ == "__main__":
main()
And my PHP:
$python = "py";
$script = "test.py";
exec("$python $script 2>&1", $output);
print_r($output);
But when I run it in PHP using WAMP, I don't get any output from that script, nothing is outputted.
If I change my python script to only contain "print("TESTING")" then I can read that output fine in PHP which makes me think that my python code is failing perhaps due to permissions. So I changed the user from SYSTEM to my own user so when I use:
echo exec("whoami") // Outputs my user account name
I thought maybe my PHP script was off, so I tried running it though the command line, but the results are what I want:
C:\wamp\www\Volume>php index.php
Array
(
[0] => firefox.exe,Spotify.exe,Microsoft.Photos.exe,Steam.exe,
)
So I'm at a loss as to why when I execute my PHP code through my browser, I am not getting any output unless my python script only contains :
print("TESTING")
What could possibly be going wrong?
EDIT
So I decided to debug this further by altering my python script to create a .txt file on my desktop, this works fine when running it through the command line. But again, when I run it through my browser/PHP, that file isn't created. So maybe I need to grant special permissions to my python script? I'm not sure why I need to do that though as I have given PHP my user account
So I think I found out why I'm not getting any output from my python script, thanks to #Torxed.
It seems like when I run the Python script through WAMP/PHP it must run as a different user/environment which doesn't have any Audio.
This is odd however as I've set 'wampapache64' to run as my user account, even after a restart I'm still getting the same results.
I've even tried
runas /savecred /noprofile /user:<USER>
But that just returns the password prompt which I won't be able to fill out in PHP.
This project looks like a dead end for now.

Python script won't exec from php when specific library is used

Im trying to execute a Python script to get and set the parameter server in ROS. When I run any other simple python script that for example prints "Hello", I get back the value in PHP. But when I run this code:
#!/usr/bin/env python
import roslib
import rospy
import sys
import re
import string
get = rospy.get_param("param")
print get
I get an empty echo. However this code works fine in the terminal!
All I'm doing in PHP is:
$output = exec("python path/script.py")
echo $output;
I tried shell_exec, I tried with python in the command, and without. I also tried /usr/bin/python still it won't work for this specific code, but everything works for a simple print!
It turns out that Apache's user doesnt have the required environment variables. So you have to add the environment variables paths you need in Apache's.. You can set them out here

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.

Categories