HTTPリクエストハンドラ:ディレクトリ一覧

HTTPリクエストハンドラにディレクトリ一覧機能だけ追加

import StringIO
..
    def send_head(self):
        path = "." + self.path
        if os.path.isdir(path):
            return self.list_directory(path)
..
    def list_directory(self, path):
            f = StringIO.StringIO()
            f.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\"><html>\n")
            f.write("<title>Directory listing for %s</title>\n" % self.path)
            f.write("<body>\n")
            f.write("<h2>Directory listing for %s</h2>\n" % self.path)
            f.write("<hr>\n<ul>\n")
            for name in os.listdir(path):
                f.write("<li><a href=\"%s\">%s</a></li>\n" % (name, name))
            f.write("</ul>\n<hr>\n</body>\n</html>\n")
            self.send_response(200)
            self.send_header("Content-type", "text/html") 
            self.send_header("Content-Length", f.tell())
            self.end_headers()
            f.seek(0)
            return f