Shutting down computer from a python script in apache

半腔热情 提交于 2019-12-24 09:18:27

问题


I have a headless Raspberry Pi which I want to have an easy means for my child to power down. I tried the following script in the apache web server:

import os
sss = os.popen('echo password | sudo -S shutdown -hP now')
print sss.read()

But nothing happens.

Then I tried:

from subprocess import Popen, PIPE, STDOUT
p = Popen('echo password | sudo -S shutdown -hP now', shell=True, stdOUT=PIPE, stderr=STDOUT)
print p.stdout.read()

Also, nothing was output and no work appears to have been done.

How can I do a shutdown from a web page?


回答1:


For security reasons Apache user cannot run sudo command.

Usually this is an almost mandatory rule to save you from attacks but for a Raspberry PI installation you may not have this problem so you can just add Apache user to sudoers (or, better, uso visudo to edit that file).

Something else? Yes you may simply add shutdown permissions to Apache user, follow this tutorial. In short you have to change /etc/sudoers with:

%groupName ALL= NOPASSWD: /sbin/shutdown


来源:https://stackoverflow.com/questions/20066583/shutting-down-computer-from-a-python-script-in-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!