Skip to content

Instantly share code, notes, and snippets.

@tkgwy
Last active April 26, 2019 09:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkgwy/45f95f2e77bd4b0602a57867677e5f50 to your computer and use it in GitHub Desktop.
Save tkgwy/45f95f2e77bd4b0602a57867677e5f50 to your computer and use it in GitHub Desktop.
SimpleHTTPServer for SPA
import os
import sys
import urlparse
import SimpleHTTPServer
import BaseHTTPServer
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
url = urlparse.urlparse(self.path)
request_file_path = url.path.strip('/')
if not os.path.exists(request_file_path):
self.path = 'index.html'
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
host = '0.0.0.0'
try:
port = int(sys.argv[1])
except IndexError:
port = 8000
httpd = BaseHTTPServer.HTTPServer((host, port), Handler)
print 'Serving HTTP on %s port %d ...' % (host, port)
httpd.serve_forever()
import os
import sys
from urllib.parse import urlparse
from http.server import SimpleHTTPRequestHandler
from http.server import HTTPServer
class Handler(SimpleHTTPRequestHandler):
def do_GET(self):
url = urlparse(self.path)
request_file_path = url.path.strip('/')
if not os.path.exists(request_file_path):
self.path = 'index.html'
return SimpleHTTPRequestHandler.do_GET(self)
host = '0.0.0.0'
try:
port = int(sys.argv[1])
except IndexError:
port = 8000
httpd = HTTPServer((host, port), Handler)
print('Serving HTTP on %s port %d ...' % (host, port))
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment