From d2ee7f153e4d21879e12fe25ec2128f8e6466afb Mon Sep 17 00:00:00 2001 From: UltraQbik Date: Wed, 21 Aug 2024 00:54:27 +0300 Subject: Add simple response.html page --- main.py | 17 ++++++++++- www/err/response.html | 10 +++++++ www/index.html | 78 +++++++++++++++++++++++++-------------------------- 3 files changed, 65 insertions(+), 40 deletions(-) create mode 100644 www/err/response.html diff --git a/main.py b/main.py index 491f85e..f99fae8 100644 --- a/main.py +++ b/main.py @@ -21,6 +21,11 @@ PATH_MAP = { "/css/styles.css": {"path": "css/styles.css"}, } +# internal path map +I_PATH_MAP = { + "/err/response.html": {"path": "www/err/response.html"} +} + def get_response_code(code: int) -> bytes: match code: @@ -200,8 +205,18 @@ class HTTPServer: # send 200 response with the file to the client HTTPServer._send(client, 200, data, headers) else: + # in case of error, return error page + async with aiofiles.open(I_PATH_MAP["/err/response.html"]["path"], "r") as f: + data = await f.read() + + # status code + status_code = 404 + + # format error response + data = data.format(status_code=get_response_code(status_code).decode("ascii")) + # send 404 response to the client - HTTPServer._send(client, 404) + HTTPServer._send(client, status_code, data.encode("ascii")) @staticmethod def _send(client: socket.socket, response: int, data: bytes = None, headers: dict[str, str] = None): diff --git a/www/err/response.html b/www/err/response.html new file mode 100644 index 0000000..70753e2 --- /dev/null +++ b/www/err/response.html @@ -0,0 +1,10 @@ + + + + {status_code} + + +
{status_code}
+

+ + \ No newline at end of file diff --git a/www/index.html b/www/index.html index 6ba0e5b..bdb132f 100644 --- a/www/index.html +++ b/www/index.html @@ -1,47 +1,47 @@ - - The YES's Page - - - + + The YES's Page + + +
Welcome To The Mighty HTML Page
-
-
-

What is this?

-

> This is an HTML page!

-

> It exists!

-
-
-

Why does this exist?

-

> Funny haha

-

> I'm learning how to webpage from nothing

-
-
-

What is it running?

-

> This server is run by the shitty python code I wrote

-

> Server does not use flask or any other similar python web frameworks

-

> It primarily uses standard python libraries, with 2 libraries being an exception

-

> aiofiles - for asynchronous file I/O

-
-
-

Where can I see the shitty python code you wrote?

-

> You can check the shitty python code I wrote at my github

-

> here's comically large button to help you navigate to there

-

- -

-
-
-

Useless button section?

-

- -

-
-
+
+
+

What is this?

+

> This is an HTML page!

+

> It exists!

+
+
+

Why does this exist?

+

> Funny haha

+

> I'm learning how to webpage from nothing

+
+
+

What is it running?

+

> This server is run by the shitty python code I wrote

+

> Server does not use flask or any other similar python web frameworks

+

> It primarily uses standard python libraries, with 2 libraries being an exception

+

> aiofiles - for asynchronous file I/O

+
+
+

Where can I see the shitty python code you wrote?

+

> You can check the shitty python code I wrote at my GitHub

+

> here's comically large button to help you navigate to there

+

+ +

+
+
+

Useless button section?

+

+ +

+
+
- + -- cgit 1.4.1