从 apache 中的 python 脚本关闭计算机 [英] Shutting down computer from a python script in apache

查看:29
本文介绍了从 apache 中的 python 脚本关闭计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无头的 Raspberry Pi,我想用一种简单的方法让我的孩子断电.我在 apache 网络服务器中尝试了以下脚本:

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()

但什么也没发生.

然后我尝试了:

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.

如何从网页关闭?

推荐答案

出于安全原因,Apache 用户无法运行 sudo 命令.

For security reasons Apache user cannot run sudo command.

通常这是一个几乎强制性的规则,可以使您免受攻击,但对于 Raspberry PI 安装,您可能不会遇到此问题,因此您只需将 Apache 用户添加到 sudoers(或者,更好的是,使用 visudo> 编辑该文件).

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).

还有什么?是的,您可以简单地为 Apache 用户添加关闭权限,请遵循本教程.简而言之,您必须将 /etc/sudoers 更改为:

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

这篇关于从 apache 中的 python 脚本关闭计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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