Strange issue running Python script in php in browser - php

I am using python's nltk to do some interesting things to input strings from php, but I'm experiencing some difficulties in getting it to output to the browser. My code looks like this:
system("python test.py command line arguments");
which outputs correctly in the command line no matter what is in my test.py file. However, in the browser it will work unless specific code is present.
inside test.py:
import string
import sys
from nltk import word_tokenize as wt
from nltk.corpus import wordnet as wn
x = ""
for item in sys.argv: x += item+" "
i = wt(x)
result = ""
wn.morphy(i[0])
print "hello world"
This will not print "hello world" in the browser, but only if the preceding line of code is present. without
wn.morphy
it will run. Unfortunately I need to use wn.morphy. Why would this not work in the browser with that specific line of code in my test.py file? I have already turned off safe mode, which is what allows me to output to the browser with python at all

Actually, this code works for me on the PHP CLI, whether or not I include the wn.morphy line, so I think your problem is elsewhere, or version-related.
Did you remember to download the 'wordnet' corpus? If not, run
>>> import nltk
>>> nltk.download('wordnet')
in Python, and then try again. I could see the script erroring out because it can't get to the corpus.

What is the error? Could it be due to the size of WordNet (which is pretty big) or the time it takes Python/NLTK takes to load it? A live web server is probably going to have tighter quotas than an interactive console session.

Related

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

Apache doesn't let PIL work

I wrote "Apache doesn't let PIL work" as title but I'm not sure about if this is the case or not.
I have a php script that sends json data to a py file that will process an image according to this json data. The communication between languages works. But when I add the line for importing PIL to py file, php returns NULL on browser. It's also working when I run the php script with command line but I have to run it on web browser.
This is the php script:
<?php
$result = exec('python somecode.py ' . escapeshellarg(json_encode('897')));
$resultData = json_decode($result, true);
var_dump($resultData);
?>
this is the py file:
import sys, json
data = json.loads(sys.argv[1])
print data
this works just fine and gives me the output of int(897) which is expected. When I add the line of from PIL import Image, it gives NULL on browser but it still can being ran on command line without any problem. I didn't even add the code for processing image, just trying to import PIL.
EDIT: I tried to import numpy, it didn't prevent the code from running. It runs on browser even I import numpy. So, probably problem is related with PIL.
It was about the image's path. I changed it to a proper one, and the problem is fixed.

PHP's virtual() command causes raw Python code to display on the webpage instead of executing it

I'm using this command to execute a Python script from who.php:
virtual('../cgi-bin/sigphon/who.py');
on an Apache server, but when I go to the webpage from which command is called (who.php), raw code from the Python file is displayed, like this:
\#!/usr/bin/python print '3' scriptpath = "/compsci/webdocs/morphon/web_docs/cgi-bin/sigphon" import sys, cgi, string, sha, time import cgitb sys.path.insert(0, scriptpath) import easyhtml, member, readdb, common cgitb.enable() # FieldStorage object to hold the form data formdata = cgi.FieldStorage() thisscript = "%s/who.py" % (common.cgibase) def update_text(): return """
etc. ...
I'm able to execute the who.py script from the command line on the same server, from the same directory as who.php, with no issues, and I've tried setting the permissions for all files involved to 777.
I've also tried
echo exec('../cgi-bin/sigphon/who.py');
but to no avail. What's going on here?
You would have to configure Apache to parse .py files using python. There is a mod_python for Apache or you can set it up to run under CGI.
Most likely you can just use one of the PHP Program Execution Functions.
passthru('/path/to/python ../cgi-bin/sigphon/who.py');

PHP not executing python script correctly

I'm executing a python script from a php page like so: exec("python ./test.py");
This script runs fine if I don't open a serial port in it. If I do, however, (and this is the whole point of calling the python script in the first place), the script doesn't execute properly.
If I call a simple python script that prints a statement -
print "This works!"
Then I get the desired output in my php page.
But, if I open a serial port, I no longer get the output of "This works!", and the serial data is not getting sent to the receiving device -
import serial
ser = serial.Serial("/dev/ttyACM0",9600)
print "This works!"
Both scripts run fine from the command line.
Is this a php limitation? I have tried other methods of execution such as popen and system, but they didn't work for me either.
Perhaps you aren't getting complete error reporting from your Python execution. Try adding raise Exception('Boo!') as the first line of you Python program to find out if you are or not. If you don't get the exception and a traceback, then your program is probably failing on the serial.Serial line, but you aren't hearing about it.

Categories