Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

includes with SimpleHTTPServer?

I have begun using the Python SimpleHTTPServer in Mac OS X Bash to help with front-end templating rather than MAMP. I like the simplicity but wondered if there is a way to use includes for embedding repeatable parts of the page (mainly header/footer)?

I'd use PHP for this normally but I don't think that's an option with SimpleHTTPServer so i'd like to know if there any other ways to do this easily?

like image 535
Alistair Chisholm Avatar asked Feb 28 '26 12:02

Alistair Chisholm


1 Answers

You can do anything you want inside the request's do_GET() method, including parsing it for include directives as in the following code outline:

class IncludeHandler(SimpleHTTPRequestHandler):

    def do_GET(self):
        # self.path is the requested file
        complete_file = process_included_files(self.path)  # include the included files 

        # serve the file. These lines come
        # straight from the http.server source code

        self.send_response(200)
        self.send_header("Content-type", "text/html") # or whatever the mime type is
        fs = os.fstat(complete_file.fileno())
        self.send_header("Content-Length", str(fs[6]))
        self.end_headers()

        self.copyfile(complete_file, self.wfile)
like image 116
ocho88 Avatar answered Mar 03 '26 01:03

ocho88



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!