从web表单以root身份调用CGI python脚本

我很难得到一个Web窗体来运行一个CGI脚本,需要执行一个具有root权限的命令。 表格是:

<form class="mainform" action="./py/print_msg.py" method="POST"> <textarea name="post" cols=70 rows=20></textarea><br> <input type="submit" value="Print" class="button"> <input type="reset" value="Clear" class="button"> </form> 

python脚本print_msg.py是可执行文件( -rwxr-xr-x 1 root root ),并且只是打印string到浏览器的版本就可以正常工作,所以apache的CGI设置就好了。

 #!/usr/bin/env python from Adafruit_Thermal import * print "Content-type: text/html\n\n" printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) printer.wake() time.sleep(1) print "test" printer.sleep() 

这在root运行时有效。 我编辑了/etc/sudoers如下:

 www-data ALL=(ALL) NOPASSWD: /usr/bin/sudo -u root /home/pi/www/html/py/print_msg.py 

不知道这是否意味着万维网数据可以运行该脚本作为根或如果它会这样做,而不需要调用sudo ? 现在,当我打电话给我的网页时:

 Traceback (most recent call last): File "/var/www/html/py/print_msg.py", line 7, in <module> printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) File "/home/pi/www/html/py/Adafruit_Thermal.py", line 73, in __init__ Serial.__init__(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__ self.open() File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open raise SerialException("could not open port %s: %s" % (self._port, msg)) serial.serialutil.SerialException: could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0' [Wed Jun 01 13:50:04.383712 2016] [cgid:error] [pid 30345:tid 2993681456] (104)Connection reset by peer: [client 192.168.1.158:51239] AH02550: Failed to flush CGI output to client, referer: http://192.168.1.174/ 

我可能没有正确的sudoers线吗? 感谢您的帮助。

如果您不想在每次需要安装时请求root访问权限,请执行以下操作: https : //github.com/quick2wire/quick2wire-gpio-admin