4

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

daisy
  • 19,459
  • 24
  • 111
  • 218

2 Answers2

5

Simple way :

Rename the index file to anything else

A more complicated Approach :

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()
Ani
  • 690
  • 7
  • 19
0

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
daisy
  • 19,459
  • 24
  • 111
  • 218