我有一个在乌贼上运行的Linux代理服务器(RaspberryPi-3)。我想使用网页上的HTML按钮启动鱿鱼服务。使用此按钮,我试图执行一个python程序来启动鱿鱼服务:
#!/usr/bin/env python
import os
os.system ("sudo service squid restart")
但是在网页上,它不起作用。
通过浏览器打开我的鱿鱼还有哪些其他选择?
答案 0 :(得分:0)
# Importing flask module in the project is mandatory
# An object of Flask class is our WSGI application.
from flask import Flask
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def start_squid():
import os
os.system ("sudo service squid restart")
return 'Success'
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()
如上所述编写一个简单的Flask api。 在Html按钮上,对该烧瓶URL进行get / post ajax调用,以便您的鱿鱼将由烧瓶控制器启动。
也在控制器中处理异常
答案 1 :(得分:0)
您可以通过使用子过程来获得结果。
import subprocess
subprocess.call(['sudo', 'squid', 'restart'])
但是,当我们在sudo中使用子进程命令时,它可能会要求输入密码来执行该命令。
答案 2 :(得分:0)
您添加了PHP标记,因此我假设您实际上使用的是PHP作为Web脚本语言。
首先,您需要使SomeTrait
用户在没有www-data
的情况下运行命令。
sudo
将以下行添加到文件中,然后保存。
sudo visudo # you edit the /etc/sudoers with this program
设置了正确的权限后,在PHP端这应该相当简单:
www-data ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart squid
shell_exec的引用:https://www.php.net/manual/en/function.shell-exec.php