summaryrefslogtreecommitdiffstats
path: root/lib.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib.py')
-rw-r--r--lib.py42
1 files changed, 29 insertions, 13 deletions
diff --git a/lib.py b/lib.py
index 499b791..7a10bf3 100644
--- a/lib.py
+++ b/lib.py
@@ -1,22 +1,31 @@
import os
import io
import re
+import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
posts = {}
gets = {}
def find_get(path):
+ """ [Internal] - Used by the Handler class to try and
+ find the correct handler for a GET request.
+ """
try:
return gets[path]()
- except KeyError:
+ except Exception as e:
+ print(type(e), e)
return "404"
def find_post(path, request):
+ """ [Internal] - Used by the Handler class to try and
+ find the correct handler for a POST request.
+ """
try:
return posts[path](request)
- except KeyError:
+ except Exception as e:
+ print(type(e), e)
return "404"
def get_relative_path():
@@ -39,7 +48,7 @@ def read_html(relative_file_path):
return html_content
def inject_external_files(html_content):
- """ Replaces {{ 'file_path' }} with the file content of that file
+ """ [Internal] - Replaces {{ 'file_path' }} with the file content of that file
path. Useful for seperation of javascript and css files.
Uses regex to capture the pattern. Here is a link to see how it
works: https://regex101.com/r/v917NK/2
@@ -67,8 +76,8 @@ def inject_external_files(html_content):
def post(route = '/'):
""" A decorator that takes in the route path as argument, and then
puts the post handler in the dictionary, with the route as a key.
- The handler will receive one argument called 'body', where the body
- of the post request will exist. """
+ The handler will receive one `str` argument called 'body',
+ where the body of the post request will exist. """
def decorator(handler_function):
posts[route] = handler_function
@@ -89,19 +98,25 @@ def get(route = '/'):
# Explanation: https://blog.anvileight.com/posts/simple-python-http-server/
+
class Handler(BaseHTTPRequestHandler):
- """ The class responsible for handling all the
- different requests made to our server
+ """ [Internal] - The class responsible for handling all the
+ different requests made to our server.
"""
def _set_headers(self):
- """ Standard header stuff that has to be done """
+ """ Sets the headers for a response. This is to tell the browser
+ the status of the request that was sent. '200' means that everything
+ went fine.
+ """
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
- """ Just serve up our one and only html page """
+ """ Tries to find the handler for this GET request. The handler can
+ return either `bytes` or `str`.
+ """
self._set_headers()
res = find_get(self.path)
if hasattr(res, 'encode'):
@@ -111,20 +126,21 @@ class Handler(BaseHTTPRequestHandler):
def do_POST(self):
- """ An async ajax request. Find the function from 'posts.py' to handle this
- particular request.
+ """ Tries to find the handler for this POST request. The handler
+ will get the 'body' of the request as the argument. The body
+ is a `str`, formatted in JSON.
"""
self.send_response(200)
self.end_headers()
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
- res = find_post(self.path, body)
+ res = find_post(self.path, body.decode('utf-8'))
self.wfile.write(res.encode())
def run_server (port = 8314):
server_adress = ('', port) # Should make the port a command line argument
server = HTTPServer(server_adress, Handler)
- print(f'Starting server on port: {port}.')
+ print('Starting server on port: {}.'.format(port))
server.serve_forever()
\ No newline at end of file