简体   繁体   中英

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:

  • app (folder with src files)
  • dist (build folder where everything is located for a host)
    • assets (css, js, etc)
    • services (html files for different services)
    • name_of_service_1.html
    • name_of_service_2.html
    • index.html
    • services.html
  • package.json
  • gulp.js

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM