#!/bin/python3 ''' Basic website server. Copyright (C) 2025 Swirly "Stoner" Curly This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . this is a basic file server. not even an upload point. the most it does is gzip compression. this is intended for websites, but you may expand upon it. if you want a file server with an upload point, you will want https://fs.swirly.architectenterprises.net/source.py instead! it will serve files from inside of the files directory relative to the current working directory if anything bad or confusing arises, contact me! (https://swirly.architectenterprises.net) ''' from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler import os, socket, gzip, mimetypes, sys mimetypes.init() class ass(BaseHTTPRequestHandler): def do_GET(self): if self.path == "/": self.send_response(200) self.send_header('content-type', 'text/html; charset=utf-8') with open("./files/index.html", "rb") as f: contents = f.read() try: if "gzip" in self.headers["Accept-Encoding"]: contents = gzip.compress(contents, compresslevel=9) self.send_header("content-encoding", "gzip") self.send_header("vary", "Accept-Encoding") except: pass self.send_header('content-length', len(contents)) self.end_headers() self.wfile.write(contents) elif self.path == "/source.py": self.send_response(200) self.send_header('content-type', 'text/plain') self.end_headers() with open(os.path.abspath(os.path.realpath(sys.argv[0])), "rb") as f: contents = f.read() self.wfile.write(contents) else: if os.path.exists(f"./files/{self.path}"): self.send_response(200) try: self.send_header('content-type', mimetypes.types_map[f".{self.path.split('.')[1]}"]) except: self.send_header('content-type', 'application/octet-binary') with open(f"./files/{self.path}", "rb") as f: contents = f.read() try: if "gzip" in self.headers["Accept-Encoding"]: contents = gzip.compress(contents, compresslevel=9) self.send_header("content-encoding", "gzip") self.send_header("vary", "Accept-Encoding") except: pass self.send_header('content-length', len(contents)) self.end_headers() self.wfile.write(contents) else: self.send_response(404) self.send_header('content-type', 'text/html') with open(f"./files/not_found.html", "rb") as f: contents = f.read() try: if "gzip" in self.headers["Accept-Encoding"]: contents = gzip.compress(contents, compresslevel=9) self.send_header("content-encoding", "gzip") self.send_header("vary", "Accept-Encoding") except: pass self.send_header('content-length', len(contents)) self.end_headers() self.wfile.write(contents) def do_HEAD(self): ass.do_GET(self) httpd = ThreadingHTTPServer(("0.0.0.0", 63304), ass) #this will serve at port 63304. replace with 80 if you don't want to type in :63304 at the end of your address. if port 80 fails, likely another (probably better) web server is using port 80. on windows you must disable the HTTP service/feature. httpd.serve_forever()