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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With