简体   繁体   English

从apache中的python脚本关闭计算机

[英]Shutting down computer from a python script in apache

I have a headless Raspberry Pi which I want to have an easy means for my child to power down. 我有一个无头的Raspberry Pi,我希望它有一种简便的方法来让孩子断电。 I tried the following script in the apache web server: 我在apache网络服务器中尝试了以下脚本:

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? 如何关闭网页?

For security reasons Apache user cannot run sudo command. 出于安全原因,Apache用户无法运行sudo命令。

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). 通常,这是一条几乎强制性的规则,可防止您受到攻击,但是对于Raspberry PI安装,您可能不会遇到此问题,因此您可以将Apache用户添加到sudoers中 (或者,更好地,使用uso visudo来编辑该文件)。

Something else? 还有吗 Yes you may simply add shutdown permissions to Apache user, follow this tutorial . 是的,您可以仅向Apache用户添加关机权限,请按照本教程进行操作 In short you have to change /etc/sudoers with: 简而言之,您必须使用以下命令更改/etc/sudoers

%groupName ALL= NOPASSWD: /sbin/shutdown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM