2015-09-03 21:09:35 +00:00
|
|
|
import http.server
|
2015-10-03 23:29:52 +00:00
|
|
|
import os
|
2015-09-03 21:09:35 +00:00
|
|
|
|
|
|
|
f = open('favicon.png', 'rb')
|
|
|
|
FAVI = f.read()
|
|
|
|
f.close()
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
# The paths of the root folder which the user may access
|
|
|
|
# Attempting to access any other files in the root folder
|
|
|
|
# will 403
|
|
|
|
OKAY_BASE_PATHS = set(x.lower() for x in ['/', '/favicon.ico'])
|
|
|
|
FORBIDDEN_PATHS = set(x.lower() for x in ['/admin'])
|
|
|
|
|
2015-09-03 21:09:35 +00:00
|
|
|
class RequestHandler(http.server.BaseHTTPRequestHandler):
|
|
|
|
def write(self, string):
|
|
|
|
if isinstance(string, str):
|
|
|
|
string = string.encode('utf-8')
|
|
|
|
self.wfile.write(string)
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
def read_filebytes(self, path):
|
|
|
|
#print(path)
|
|
|
|
if os.path.isfile(path):
|
|
|
|
f = open(path, 'rb')
|
|
|
|
fr = f.read()
|
|
|
|
f.close()
|
|
|
|
return fr
|
|
|
|
if os.path.isdir(path):
|
|
|
|
return self.read_filebytes(os.path.join(path, 'index.html'))
|
|
|
|
self.send_error(404)
|
|
|
|
return b''
|
|
|
|
|
2015-09-03 21:09:35 +00:00
|
|
|
def do_GET(self):
|
|
|
|
#print(dir(self))
|
2015-10-03 23:29:52 +00:00
|
|
|
path = self.path.lower()
|
|
|
|
if os.path.dirname(path) in FORBIDDEN_PATHS:
|
|
|
|
self.send_error(403, 'Forbidden path!')
|
|
|
|
return
|
|
|
|
if path not in OKAY_BASE_PATHS and (os.path.dirname(path) == '/'):
|
|
|
|
self.send_error(403, 'Stop that!')
|
|
|
|
return
|
|
|
|
path = os.path.join(os.getcwd(), path[1:])
|
|
|
|
d = self.read_filebytes(path)
|
|
|
|
self.write(d)
|
2015-09-03 21:09:35 +00:00
|
|
|
|
|
|
|
server = http.server.HTTPServer(('', 80), RequestHandler)
|
|
|
|
print('server starting')
|
|
|
|
server.serve_forever()
|