NoobyAFK
NoobyAFK

Reputation: 317

Using Python SimpleHTTPServer to serve files without .html

I want to use SimpleHTTPServer to serve my local site while I'm developing. I'm using basic javascript, HTML, and CSS. I have this kind of project structure:

Inside navigation I have a basic structure for every link, something like this:

<a href="/services">Services</a>
<a href="/services/name_of_service_1">Service 1</a>

Besides this, I'm using HTML preload, so that pages are loaded faster if someone hovers over those links. Because of that, I can't use services.html or etc, because in that case, preload won't work. I'm using netlify to host this site, and there everything works fine.

My question: How to serve locally with SimpleHTTPServer but that page will load nicely without .html extension in the link.

Upvotes: 3

Views: 919

Answers (1)

Emrah Diril
Emrah Diril

Reputation: 1765

Here is how to do it:

import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler

Handler.extensions_map={
    '.html': 'text/html',
    '': 'text/html', # Default is 'application/octet-stream'
    }

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

Reference

Upvotes: 1

Related Questions