0

I have set up an Apache 2 server on a Raspberry Pi with an index.php which I can access from any computer on the network. In the same folder (/var/www) I also have a python script test.py.

The script works fine when running it manually. However, I want to be able to run the script in my browser by using php. I tried this:

<?php 

$command = escapeshellcmd('test.py');
$output = shell_exec($command);
echo $output;

?>

However nothing happens. I also tried using the full filepath /var/www/test.py, but that didn't work either.

Any suggestions on how I can get this working?

1 Answer 1

1

Two things come to mind:

First, does the first line in the file contain the full path to the python interpreter? i.e:

#!/usr/bin/python

Second, does the file have the appropriate file permissions. Can you you open it with the www-data user (same user as which your apache process should be running as)?

sudo -u www-data /var/www/test.py
Sign up to request clarification or add additional context in comments.

2 Comments

I've now added the path to the python interpreter. I tried running the command, and this is the error I get: "Cannot create testVideo.raw": Permission denied. What my python script does is record a short video with my webcam and saves it as testVideo.raw. So yeah, I don't have permission to do that. Any suggestions?
test.py is trying to create the file testVideo.raw as the www-data user. You need to make sure that the directory where that file is being created allows the www-data user to write to it. Create a new private directory somewhere, change its owner using sudo chown www-data:www-data /path/to/directory, and write your raw file there.

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.