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('%s\n' % title) r.append('\n

%s

' % title) r.append('
\n\n
\n\n\n') encoded = '\n'.join(r).encode(enc, 'surrogateescape') f = io.BytesIO() f.write(encoded) f.seek(0) self.send_response(HTTPStatus.OK) self.send_header("Content-type", "text/html; charset=%s" % enc) self.send_header("Content-Length", str(len(encoded))) self.end_headers() return f with HTTPServer(('', 8000), MyHTTPRequestHandler) as server: server.serve_forever()