Newer
Older
lynxi-casic-demo / server.py
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import subprocess

# 定义HTTP请求处理器
class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # 检查路径是否为 /reboot
        if self.path == "/reboot":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(b'{"message": "Rebooting system..."}')

            # 执行重启命令
            try:
                # 判断操作系统类型并执行相应的重启命令
                if os.name == "nt":  # Windows 系统
                    subprocess.run(["shutdown", "/r", "/t", "0"])
                else:  # Linux / macOS
                    subprocess.run(["sudo", "reboot"])
            except Exception as e:
                print(f"Failed to reboot: {e}")
        else:
            # 非 /reboot 请求返回 404
            self.send_response(404)
            self.end_headers()
            self.wfile.write(b'{"message": "Not Found"}')

# 启动HTTP服务器
def run_server(host="0.0.0.0", port=5000):
    server_address = (host, port)
    httpd = HTTPServer(server_address, RequestHandler)
    print(f"HTTP server running on {host}:{port}")
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\nServer is stopping...")
        httpd.server_close()

if __name__ == "__main__":
    run_server()