52 lines
1.1 KiB
Python
Executable File
52 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from glob import glob
|
|
import http.server
|
|
import socketserver
|
|
|
|
|
|
HOST = "localhost"
|
|
PORT = 9090
|
|
|
|
|
|
class ScriptServer(http.server.SimpleHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
scr = self.path.strip("/")
|
|
if not scr.endswith(".py"):
|
|
scr += ".py"
|
|
|
|
scripts = sorted(glob("*.py"))
|
|
if scr not in scripts:
|
|
printable_scripts = "\n".join(scripts)
|
|
self.send_result(f"{scr} does not exist. choose from:\n", printable_scripts)
|
|
return
|
|
|
|
with open(scr) as f:
|
|
exec(f.read())
|
|
|
|
self.send_result(scr)
|
|
|
|
|
|
def send_result(self, *msg):
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "text/html")
|
|
self.end_headers()
|
|
msg = " ".join(msg)
|
|
msg = msg.split("\n")
|
|
msg = "<br>".join(msg)
|
|
msg = msg.encode()
|
|
self.wfile.write(msg)
|
|
print(msg)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
socketserver.TCPServer.allow_reuse_address = True
|
|
with socketserver.ThreadingTCPServer((HOST, PORT), ScriptServer) as httpd:
|
|
print(f"serving at {HOST}:{PORT}")
|
|
httpd.serve_forever()
|
|
|
|
|
|
|