Skip to content

Instantly share code, notes, and snippets.

@iOS0x00
Created December 1, 2013 13:55
Show Gist options
  • Save iOS0x00/7734040 to your computer and use it in GitHub Desktop.
Save iOS0x00/7734040 to your computer and use it in GitHub Desktop.
自定义路径的BaseHttpServer
import os
import posixpath
import urllib
import socket
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
ROUTES = (
['', '/Users/neil/workspaces/www-data/'],
)
class RequestHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
root = os.getcwd()
print ROUTES
for pattern, rootdir in ROUTES:
if path.startswith(pattern):
path = path[len(pattern):]
root = rootdir
break
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = root
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
if __name__ == '__main__':
BaseHTTPServer.test(RequestHandler, BaseHTTPServer.HTTPServer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment