first try
This commit is contained in:
5
example1.py
Executable file
5
example1.py
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
print("running example 1")
|
||||
|
||||
|
5
example2.py
Executable file
5
example2.py
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
print("running example 2")
|
||||
|
||||
|
51
pier.py
Executable file
51
pier.py
Executable file
@ -0,0 +1,51 @@
|
||||
#!/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()
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user