Skip to content

Instantly share code, notes, and snippets.

@sunflsks
Last active July 3, 2024 09:02
Show Gist options
  • Save sunflsks/124e671bc847179c1df45c0dc757fe16 to your computer and use it in GitHub Desktop.
Save sunflsks/124e671bc847179c1df45c0dc757fe16 to your computer and use it in GitHub Desktop.
pubgist
#!/usr/bin/env python3
import http.server
import socketserver
import subprocess
import json
PORT = 80
COMMAND = "systemctl is-active automator"
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
result = subprocess.run(COMMAND, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode('utf-8').strip()
returncode = result.returncode
response = {'return_code': returncode, 'output': output}
self.send_response(200)
# Send headers
self.send_header('Content-type', 'application/json')
self.end_headers()
# Send the response
self.wfile.write(json.dumps(response).encode('utf-8'))
handler_object = RequestHandler
with socketserver.TCPServer(("", PORT), handler_object) as httpd:
print("Serving on port", PORT)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment