FileServer

This commit is contained in:
Roman Hergenreder 2020-09-27 14:00:20 +02:00
parent 5dcbdb5f86
commit 3adb813560
2 changed files with 31 additions and 1 deletions

@ -1,3 +1,3 @@
__doc__ = __doc__ or ""
__all__ = ["util"]
__all__ = ["util","fileserver"]

30
fileserver.py Normal file

@ -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)