This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
isitstilup/main.py
2023-07-20 16:48:51 +00:00

40 lines
982 B
Python

import os, subprocess, socketserver
from io import StringIO
from http.server import BaseHTTPRequestHandler
from typing import Type, Any
Serv: Type[socketserver.BaseServer] = socketserver.TCPServer
PORT = os.environ['PORT']
if '/' in PORT:
try: os.unlink(PORT)
except OSError: pass
Serv = socketserver.UnixStreamServer
ADDR: Any = PORT
else:
Serv = socketserver.TCPServer
ADDR = ("", int(PORT))
# fix cwd
os.chdir(os.path.dirname(__file__))
head_text = b"""\
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.11.3
Content-type: text/plain; charset=utf-8
"""
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.wfile.write(head_text)
subprocess.run(['python', 'status.py'], stdout=self.wfile)
def do_HEAD(self):
self.wfile.write(head_text)
with Serv(ADDR, Handler) as httpd:
print("serving at port", PORT)
if Serv == socketserver.UnixStreamServer:
os.chmod(PORT, 0o660)
httpd.serve_forever()