Skip to content

Instantly share code, notes, and snippets.

@faelp22
Created December 30, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faelp22/d57e52967a3a7e4d98e8f3ce9c5863cc to your computer and use it in GitHub Desktop.
Save faelp22/d57e52967a3a7e4d98e8f3ce9c5863cc to your computer and use it in GitHub Desktop.
I made an adjustment for Python 3, I don't know if it's very good
#!/usr/bin/env python
'''
Taken from:
http://stackoverflow.com/users/1074592/fakerainbrigand
http://stackoverflow.com/questions/15401815/python-simplehttpserver
https://gist.github.com/chrisbolin/2e90bc492270802d00a6
'''
import os
from http.server import SimpleHTTPRequestHandler
import socketserver as SocketServer
from urllib.parse import urlparse
PORT = 8000
INDEXFILE = 'index.html'
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
# Parse query data to find out what was requested
parsedParams = urlparse(self.path)
# See if the file requested exists
if os.access('.' + os.sep + parsedParams.path, os.R_OK):
# File exists, serve it up
SimpleHTTPRequestHandler.do_GET(self);
else:
# send index.html, but don't redirect
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
with open(INDEXFILE, 'rb') as fin:
self.copyfile(fin, self.wfile)
Handler = MyHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment