Skip to content

Instantly share code, notes, and snippets.

@JorgeJuarezM
Forked from iktakahiro/server4spa.py
Created November 3, 2023 01:00
Show Gist options
  • Select an option

  • Save JorgeJuarezM/e7037ca076d90b60d207d80e22ab96fb to your computer and use it in GitHub Desktop.

Select an option

Save JorgeJuarezM/e7037ca076d90b60d207d80e22ab96fb to your computer and use it in GitHub Desktop.

Revisions

  1. @iktakahiro iktakahiro revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server4spa.py
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ def do_GET(self):

    ext = request_file_path.suffix
    if not request_file_path.is_file() and not pattern.match(ext):
    self.path = '/index.html'
    self.path = 'index.html'

    return http.server.SimpleHTTPRequestHandler.do_GET(self)

  2. @iktakahiro iktakahiro created this gist Mar 18, 2016.
    29 changes: 29 additions & 0 deletions server4spa.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env python

    # Inspired by https://gist.github.com/jtangelder/e445e9a7f5e31c220be6
    # Python3 http.server for Single Page Application

    import urllib.parse
    import http.server
    import socketserver
    import re
    from pathlib import Path

    HOST = ('0.0.0.0', 8000)
    pattern = re.compile('.png|.jpg|.jpeg|.js|.css|.ico|.gif|.svg', re.IGNORECASE)


    class Handler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
    url_parts = urllib.parse.urlparse(self.path)
    request_file_path = Path(url_parts.path.strip("/"))

    ext = request_file_path.suffix
    if not request_file_path.is_file() and not pattern.match(ext):
    self.path = '/index.html'

    return http.server.SimpleHTTPRequestHandler.do_GET(self)


    httpd = socketserver.TCPServer(HOST, Handler)
    httpd.serve_forever()