1

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

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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...

Comments

0

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().

2 Comments

Hi TML, with passthru, same outcome is observed. Also, chmod 755 has been performed across all .py files in /var/www/cgi-bin Thanks 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.
The issue will be with the write permission of the output directory, write permission on the script is irrelevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.