从Web服务器运行系统调用 [英] Running system calls from webserver

查看:133
本文介绍了从Web服务器运行系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从cgi python脚本运行标准的python os.system调用. 这是教程的一部分,因此脚本非常简单.我正在尝试使用Raspberry Pi相机拍摄照片并将其显示在网页中.

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 Raspberry Pi camera and display it in a webpage.

import os, sys

os.system('raspistill -o /var/www/images/image.jpg')

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '<img src="/var/www/images/image.jpg"/>'
print '</body>'
print '</html>'

运行系统命令(在浏览器中打开)时出现的错误是:
*无法打开vchiq实例"

The error I get when running the system command (opening in a browser) is:
* failed to open vchiq instance"

我看到了另一个问题,它说明以错误的用户身份运行http守护进程,但我不确定这是什么意思. 当我以标准用户身份运行脚本时,脚本运行良好.

I have seen another question on this and it said something about the http deamon running as the wrong user, but I am not sure what that meant. The scripts runs fine when I am running it as the standard user.

推荐答案

我已修复它.

Web服务器可以访问raspistill命令,但是该命令使用了它无法访问的视频设备.我将www数据用户添加到了视频和音频组中,因此我既可以播放音频也可以拍照.我还必须更改Web目录中某些文件夹的某些组. 我必须修复的最后一件事是os.system()调用返回了某些内容,这使浏览器在显示网页时遇到了一些问题.它仅显示文本.我现在使用子流程模块,初始代码似乎可以正常工作.我的简单测试代码在这里:

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 for CGI 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)

这篇关于从Web服务器运行系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆