By default, SimpleHTTPServer uses index.html
as a index file, I would like to disable them and always show a directory index.
How can I do that? The document here says nothing about it
By default, SimpleHTTPServer uses index.html
as a index file, I would like to disable them and always show a directory index.
How can I do that? The document here says nothing about it
Rename the index file to anything else
You would have to override the translate_path method of SimpleHTTPRequestHandler with something like this:
import BaseHTTPServer
import SimpleHTTPServer
server_address = ("", 8888)
PUBLIC_RESOURCE_PREFIX = '/public'
PUBLIC_DIRECTORY = '/path/to/protected/public'
class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def translate_path(self, path):
if self.path.startswith(PUBLIC_RESOURCE_PREFIX):
if self.path == PUBLIC_RESOURCE_PREFIX or self.path == PUBLIC_RESOURCE_PREFIX + '/':
return PUBLIC_DIRECTORY + path[len(PUBLIC_RESOURCE_PREFIX):]
else:
return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, path)
httpd = BaseHTTPServer.HTTPServer(server_address, MyRequestHandler)
httpd.serve_forever()
I should be overwriting the send_head
method
Simply disable the following lines
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break