From 3adb81356096d6ec92227dd3fb0533b1d8972522 Mon Sep 17 00:00:00 2001 From: Roman Hergenreder Date: Sun, 27 Sep 2020 14:00:20 +0200 Subject: [PATCH] FileServer --- __init__.py | 2 +- fileserver.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 fileserver.py diff --git a/__init__.py b/__init__.py index c8033ea..c10acb8 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,3 @@ __doc__ = __doc__ or "" -__all__ = ["util"] +__all__ = ["util","fileserver"] diff --git a/fileserver.py b/fileserver.py new file mode 100644 index 0000000..f7c6244 --- /dev/null +++ b/fileserver.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +from http.server import BaseHTTPRequestHandler, HTTPServer + +class HttpFileServer(BaseHTTPRequestHandler): + + def __init__(self): + self.logRequests = False + self.files = { } + + def addFile(self, name, data): + if isinstance(data, str): + data = data.encode("UTF-8") + if not name.startswith("/"): + name = "/" + name + self.files[name.strip()] = data + + def do_GET(self): + if self.path in self.files: + data = self.files[self.path] + self.send_response(200) + self.end_headers() + self.wfile.write(data) + else: + self.send_response(404) + self.end_headers() + + def log_message(self, format, *args): + if self.logRequests: + BaseHTTPRequestHandler.log_message(format, *args)