Is SimpleHTTPServer included in the package?

Lately, I've been experimenting with the Python SimpleHTTPServer in Mac OS X Bash instead of MAMP to assist with front-end templating. I appreciate its user-friendly nature, but I'm curious if there is a method to incorporate includes for repeated sections of the page like headers and footers.

Typically, I would utilize PHP for this task. However, since it's not compatible with SimpleHTTPServer, I'm interested in exploring other straightforward alternatives. Any suggestions on how to achieve this seamlessly?

Answer №1

If you find yourself inside the request's do_GET() method, there are numerous possibilities at your disposal. One option is to parse it for include directives, much like the code outline provided below:

class IncludeHandler(SimpleHTTPRequestHandler):

    def do_GET(self):
        # The self.path variable stores the requested file
        complete_file = process_included_files(self.path)  # Includes the necessary files 

        # Serve the file. The following lines have been extracted directly from the http.server source code

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

        self.copyfile(complete_file, self.wfile)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trigger a fixed bottom bar animation upon hover

I have a bar fixed to the bottom of the browser that I want to hide by default. When a user hovers over it, I want the bar to be displayed until they move their cursor away. <!doctype html> <html> <head> <meta charset="utf-8"> &l ...

Using Bootstrap multiselect, you can easily control the display of a second menu based on the selection in the first menu

On my website, I am working with two menus. When I select Abon from the first menu, it should display all li elements under the Abon optgroup (Abon-1, Abon-2). If I uncheck block in the second menu, those elements should disappear. The code consists of Sel ...

Extracting Booking.com's Availability Information through Web Scraping

I'm currently trying to extract availability information from booking.com but I've encountered an issue when trying to click and open the dates tab. Despite my efforts, I can't figure out why I keep getting an error. Below is the code snipp ...

Selecting radio button does not update corresponding label

I am having an issue with setting a radio button as checked. In the example snippet, it works perfectly fine but on my localhost, it is not working. Even though the input gets checked, the label does not change. Surprisingly, if I set another radio button ...

Traversing a class object dictionary without using mixins in Python

The primary function of the class involves a dictionary where words are utilized as keys and ID numbers assigned as values (note: ID numbers may not be sequential due to possible removal of entries). x = {'foo':0, 'bar':1, 'king&a ...

Grouping pandas dataframes and appending values to distinct columns

Currently, I am working with a pandas dataframe which is displayed as follows: https://i.stack.imgur.com/K3XoT.png I am looking to get the output in the format shown here: https://i.stack.imgur.com/WyH19.png Your assistance on this matter would be high ...

Is it possible to convert HTML to PDF on the server?

A PDF file is being created from an HTML file using the princexml pdf converter package. The data for the HTML file is provided by the server. In the browser, jQuery is used to generate the input string (HTML code) for creating the PDF. Once the input stri ...

Steps to generate a list with nested lists, each containing data from a CSV document

Hey everyone, I'm in need of some assistance with creating a nested list that pulls information from a CSV file. Below is a snippet of the CSV file: ,aecousticnss,danceability,duration_ms,energy,instrumentalness,key,liveness,loudness,mode,speechiness ...

Sorting custom objects in a specific order

I am dealing with a list of objects like: actors = [Person('Raj' ,'Hindi'), Person('John', 'English'), Person('Michael' 'Marathi'), Person('Terry','H ...

Background image that covers the entire page

I am using background-size:cover to ensure that the entire background positioning area is covered by the background image. However, I've noticed that setting it to "cover" sometimes cuts off parts of my background image. Is there another way to achiev ...

Learn the steps to utilize pywinauto and selenium for logging into any account

I attempted to create a script for logging in using pywinauto and selenium, but it failed. I searched for a solution, but it seems there isn't much information available on this topic. from pywinauto import application from selenium import webdriver ...

Is there a way to achieve horizontal alignment for the Twitter and Facebook buttons on my page?

By utilizing the html code provided, I successfully incorporated Twitter and Facebook "Like" buttons into my website. Initially, they were stacked vertically, which resulted in excessive use of vertical space. To optimize space usage, I decided to align th ...

How can AJAX be utilized with both file and text inputs?

I have a basic form enclosed in a modal window, dynamically generated using jQuery: <form name="altEditor-form" id="altEditor-form" enctype="multipart/form-data" role="form"> <div class="form-group"> <div class="col-sm-5 col-md- ...

What is the reason behind IE's decision to reduce the size of password boxes compared to text

Take a look at the uncomplicated form right below. You'll notice that it consists of a text box positioned above a password box. In older versions of Internet Explorer like 7 and 8, as well as possibly in other browsers, you might observe that the tex ...

p5.js experiencing issue: Uncaught TypeError - Unable to access property 'transpose3x3' due to null value

I have a simple website built with Flask where I am running several p5.js sketch animations, and everything is working smoothly. However, when I try to add a new sketch that utilizes WEBGL, I encounter the following error: Uncaught TypeError: Cannot read p ...

CSS behaving strangely with the height property

I've run into a specific issue with a div element that contains multiple buttons. I am trying to adjust the height and position of one button so that it takes up space above the other buttons, but every time I make changes to its height and position, ...

Extracting data from SVG files using Python

Is there a way to extract coordinates/paths from an SVG file using python (I think it's located under the "path" ID, specifically the d="..."/>) for use in controlling a 2 axis CNC machine? I've looked on various platforms like SO and Google for ...

What is causing this unexpected text to display in Django when encountering errors while trying to submit a form?

On my website, there is a form that collects an employee number and validates it against another model called Salesman. It also checks if the employee's team field includes 'WF'. The validation process functions correctly and displays everyt ...

What is the best way to incorporate a search feature within Bootstrap cards?

I've been struggling to incorporate a search feature within my bootstrap cards. Despite trying various online methods, none have proven successful for me so far. Below is my HTML code - any assistance in implementing a search filter into my app would ...

How to position text in the center of a video using CSS

My attempt to center the name of my blog on top of a video background was not successful, even though I tried positioning it absolutely with 50% from the top. Can someone assist me in vertically and horizontally centering the text over the video? Here is ...