jdkanani / Server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/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 () |
for python3 change from BaseHTTPServer import HTTPServer and
from SimpleHTTPServer import SimpleHTTPRequestHandler to from http.server import HTTPServer and
from http.server import SimpleHTTPRequestHandler