summaryrefslogtreecommitdiffstats
path: root/lib.py
diff options
context:
space:
mode:
authorMax Bringemo <maxbr167@student.liu.se>2020-11-02 15:54:52 +0100
committerMax Bringemo <maxbr167@student.liu.se>2020-11-02 15:54:52 +0100
commit5a25e55b0fd59a032c4c85af452bd433427c3890 (patch)
tree84252877002e3eec3f58f92d614c225557a21156 /lib.py
parent58f04e8c6d768fc8b912ec1af44442eebac03595 (diff)
downloadtdde25-5a25e55b0fd59a032c4c85af452bd433427c3890.tar.gz
finished introductory phase
Diffstat (limited to 'lib.py')
-rw-r--r--lib.py30
1 files changed, 15 insertions, 15 deletions
diff --git a/lib.py b/lib.py
index 7a10bf3..4994e06 100644
--- a/lib.py
+++ b/lib.py
@@ -1,12 +1,11 @@
-import os
-import io
+import os
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.
@@ -28,6 +27,7 @@ def find_post(path, request):
print(type(e), e)
return "404"
+
def get_relative_path():
return os.path.dirname(os.path.abspath(__file__))
@@ -39,13 +39,14 @@ def read_html(relative_file_path):
Example:
>>> html_string = read_html('templates/index.html')
- """
+ """
dir_path = get_relative_path()
- file_path = os.path.join(dir_path, relative_file_path)
+ file_path = os.path.join(dir_path, relative_file_path)
with open(file_path) as file:
html_content = file.read()
html_content = inject_external_files(html_content)
- return html_content
+ return html_content
+
def inject_external_files(html_content):
""" [Internal] - Replaces {{ 'file_path' }} with the file content of that file
@@ -71,9 +72,9 @@ def inject_external_files(html_content):
to_be_replaced = '{{' + match + '}}'
new_html_content = new_html_content.replace(to_be_replaced, file_content)
return new_html_content
-
-def post(route = '/'):
+
+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 `str` argument called 'body',
@@ -81,18 +82,19 @@ def post(route = '/'):
def decorator(handler_function):
posts[route] = handler_function
- return handler_function
+ return handler_function
return decorator
-def get(route = '/'):
+
+def get(route='/'):
""" A decorator that takes in the route path as argument, and then
puts the get handler in the dictionary, with the route as a key.
The handler function should return either `bytes` or `str`. """
def decorator(handler_function):
gets[route] = handler_function
- return handler_function
+ return handler_function
return decorator
@@ -124,7 +126,6 @@ class Handler(BaseHTTPRequestHandler):
else:
self.wfile.write(res)
-
def do_POST(self):
""" Tries to find the handler for this POST request. The handler
will get the 'body' of the request as the argument. The body
@@ -138,9 +139,8 @@ class Handler(BaseHTTPRequestHandler):
self.wfile.write(res.encode())
-def run_server (port = 8314):
- server_adress = ('', port) # Should make the port a command line argument
+def run_server(port=8314):
+ server_adress = ('', port) # Should make the port a command line argument
server = HTTPServer(server_adress, Handler)
print('Starting server on port: {}.'.format(port))
server.serve_forever()
- \ No newline at end of file