from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler import os import urllib.parse import html from http import HTTPStatus import io import sys def is_picture(filename): file_ext=os.path.splitext(filename)[1] if file_ext in ['.bmp','.png','.jpg','.jpeg','.gif']: return True return False class MyHTTPRequestHandler(SimpleHTTPRequestHandler): def list_directory(self, path): """Helper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head(). """ try: list = os.listdir(path) except OSError: self.send_error( HTTPStatus.NOT_FOUND, "No permission to list directory") return None list.sort(key=lambda a: a.lower()) r = [] try: displaypath = urllib.parse.unquote(self.path, errors='surrogatepass') except UnicodeDecodeError: displaypath = urllib.parse.unquote(path) displaypath = html.escape(displaypath, quote=False) enc = sys.getfilesystemencoding() title = 'Directory listing for %s' % displaypath r.append('') r.append('\n
') r.append('' % enc) r.append('