Skip to content Skip to sidebar Skip to footer

Running System Calls From Webserver

I am trying to run a standard python os.system call from a cgi python script. This is part of a tutorial so the script is quite simple. I am trying to take a picture with the Raspb

Solution 1:

Most web servers run with a user for the webserver. Apache2 runs as www-data, for instance. All files in the computer have ownership and permission data for them that would allow or disallow certain operations from different users - for instance only the superuser (root) can run the poweroff application to turn off the computer.

What you should do is locate the executable you're trying to run which raspistill. This will return the location of the executable file. Next you should check the file permissions using ls -l `which raspistill` and see the owner data and file permissions which are displayed as -rwxr-xr-- (This is a common permission set, yours might vary). The 1st 3 represent Read-Write-eXecute permissions for the file owner, the next 3 chars represent only Read and eXecute permissions for the user group and the last 3 chars represent only Read permissions for the "other" users.

If the owner of the file is not www-data you could do several things such as change the ownership information for the file using chown <user> <file> which I don't recommend or adding execution privileges to the "other" user set with chmod o+x `which raspistill`.

If the problem is indeed with the permissions - this should solve your problem.

Additional information:

http://www.computerhope.com/unix/uchmod.htm

http://www.ss64.com/bash/chmod.html

Solution 2:

I fixed it.

The web server had access to the raspistill command, but that command used a video device that it did not have access to. I added the www-data user to the video and the audio group so I could both play audio and take pictures. I also had to change some groups for some folders in my web-directory. The last thing I had to fix was that the os.system() call returned something and that gave the browser some problems with displaying the webpage. It only displayed text. I now use the subprocess module and the initial code seems to work. My simple test code is here:

import os, sys

import subprocess

#output = subprocess.check_output("raspistill -o /var/www/images/image.jpg",     shell=True)
#os.system('raspistill -v -o /var/www/images/image.jpg')

# Import modules forCGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

output =""
output2 =""
# Get data from fields
if form.getvalue('speak_en'):
   output = subprocess.check_output("espeak \"%s\""% (form.getvalue('speak')), shell=True)

if form.getvalue('picture'):
   output2 = subprocess.check_output("raspistill -o /var/www/images/image.jpg", shell=True)


print """\
Content-type:text/html\n
<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>Select photo or speak</h2>
<form action=\"/cgi-bin/hello.py\" method=\"post\">
<input type=\"checkbox\" name=\"speak_en\" value=\"on\" />
Speak: <input type=\"text\" name=\"speak\"><br />
Take picture:
<input type=\"checkbox\" name=\"picture\" value=\"on\" />
<br />
<input type=\"submit\" value=\"Submit\" />
</form>
<img src=\"../images/image.jpg\" width=640 height=480>
<p>Speak output: %s</p>
<p>Picture output: %s</p>
</body>
</html>
"""% (output, output2)

Post a Comment for "Running System Calls From Webserver"