FileServer
This commit is contained in:
parent
5dcbdb5f86
commit
3adb813560
@ -1,3 +1,3 @@
|
||||
__doc__ = __doc__ or ""
|
||||
|
||||
__all__ = ["util"]
|
||||
__all__ = ["util","fileserver"]
|
||||
|
30
fileserver.py
Normal file
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)
|
Loading…
Reference in New Issue
Block a user