tags:

views:

366

answers:

4

Scenario:

I have a php page in which I call a python script.

Python script when run on the command line (Linux) shows output on the command line, as well as writes the output to a file.

Python script when run through php, doesn't do either.

Elaboration:

I use a simple system command in PHP to run the python script as:

/var/www/html/1.php: system('/usr/python/bin/python3 ../cgi-bin/tabular.py 1');

/var/www/cgi-bin/tabular.py --This python file basically parses a data file, uses python's regular expression to search for specific headings and outputs the headings to the stdout, as well as write it to a file.

This python script has a few routines in it which get executed, so I put print statements to debug. I noticed only a few initial print statements' output in the PHP page, all the ones from the function that actually does something are not seen.

Also, as part of my test, I thought well the py script is in a different folder so let me change it to the /var/www/html folder, no go.

I hope I captured the problem statement with sufficient detail and someone is able to reproduce this issue at their end. If I make any progress on this one myself, I'll annotate this question. Thanks everyone.

Gaurav

A: 

Check that the user the python script is running is has write permissions in CWD. Also, try shell_exec() or passthru() to call the script, rather than system().

TML
Hi TML, with passthru, same outcome is observed.Also, chmod 755 has been performed across all .py files in /var/www/cgi-binThanks for the response, I'm going to keep at this problem as the output of print in the .py should be visible in the .php page.
gsvolt
The issue will be with the write permission of the output directory, write permission on the script is irrelevant.
therefromhere
A: 

A permission problem is most likely the case.

If apache is running as apache, then it will not have access to write to a file unless

  1. The file is owned by apache
  2. The file is in the group apache and group writable
  3. The file is world writable

This is a "sticky" problem on a multi-user machine, as different people have access to Apache.

Try chmod 666 output.txt on the file and then re-run your test.


Considerations:

  1. Have the python script write the output to a database
  2. Use PHP's popen functionality to open the process and communicate over pipes
  3. Re-write using PHP's regular expressions
  4. Write the output file to /tmp and then read the results using PHP as soon as the python script is done.
  5. etc...
gahooa
A: 

I bet your py script has some bug which couses it to break when called from inside PHP. Try

passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');

to investigate (notice 2>&1 which causess stderr to be written to stdout).

hegemon
Thankyou hegemon. Using your advice, when I redirected the output I found the root cause - relative path for the data file in my globalvariables.py script was not being resolved correctly - I gave the complete path and this solved the problem.
gsvolt
A: 

Hi guys,

I have an almost identical problem. My python script loggs a bunch of stuff and produces about 10 output files. It also has functions which generate new files. So I have two questions:

  • is it possible to change the context which appache is running in?
  • is there another way to deal with these permission issues, such that log files can be written to and new files can be created?

Thanks for all and any help! :)

Marko