Implement /api/server/{info,packages}

This commit is contained in:
Anthony Wang 2022-05-23 23:01:33 -05:00
parent f6056797c1
commit a5e5af4cde
Signed by: a
GPG key ID: BC96B00AEC5F2D76
2 changed files with 20 additions and 3 deletions

View file

@ -1,4 +1,4 @@
from os import rename, remove, chown
from os import remove
from subprocess import run, call, check_output
from crypt import crypt
from ldappass import ldappass

21
api.py
View file

@ -1,4 +1,4 @@
from subprocess import run, check_output
from subprocess import check_output
from urllib.parse import parse_qsl
from http.server import BaseHTTPRequestHandler
@ -35,10 +35,27 @@ class api(BaseHTTPRequestHandler):
data['lastname'], data['email'], data['password'])
self.send(200, 'Well I think it worked...')
def info(self):
"""Return info about the server"""
self.send(200, check_output('neofetch | sed \'s/\\x1B\\[[0-9;\\?]*[a-zA-Z]//g\'', shell=True).decode('utf-8'))
def packages(self):
"""Return the packages installed on the server"""
self.send(200, check_output(['pacman', '-Q']).decode('utf-8'))
def do_GET(self):
"""Handle GET requests"""
self.send(200, 'Hey there, you\'ve reached the top secret API')
if self.path == '/api/server/info':
self.info()
return
elif self.path == '/api/server/packages':
self.packages()
return
self.send(501, 'We don\'t know how to do that yet')
def do_POST(self):
"""Handle API POST requests"""