40 lines
982 B
Python
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()
|