Skip to content

Instantly share code, notes, and snippets.

@blackjack4494
Forked from jdkanani/Server.py
Created June 10, 2017 03:04
Show Gist options
  • Save blackjack4494/13b1083b56f8d1ecbc425168d7935cfc to your computer and use it in GitHub Desktop.
Save blackjack4494/13b1083b56f8d1ecbc425168d7935cfc to your computer and use it in GitHub Desktop.
Python SimpleHTTPServer : Routing sample
#!/usr/bin/env python
import os
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
ROUTES = [
('/', '/var/www/doc-html')
]
class MyHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
# default root -> cwd
root = os.getcwd()
# look up routes and get root directory
for patt, rootDir in ROUTES:
if path.startswith(patt):
path = path[len(patt):]
root = rootDir
break
# new path
return os.path.join(root, path)
if __name__ == '__main__':
httpd = HTTPServer(('127.0.0.1', 8000), MyHandler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment