Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python SimpleHTTPServer : Routing sample
#!/usr/bin/env python
import os
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
('/', '/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
# new path
return os.path.join(root, path)
if __name__ == '__main__':
httpd = HTTPServer(('', 8000), MyHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment