commit 095fccc37cdaffa0267c627a43c4479aaa9649cf Author: zhangwenwen Date: Thu May 20 10:39:21 2021 +0800 initialization diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/myhttpserver.py b/myhttpserver.py new file mode 100644 index 0000000..8ac8644 --- /dev/null +++ b/myhttpserver.py @@ -0,0 +1,85 @@ +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() diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000..9abfc06 Binary files /dev/null and b/test.jpg differ