added more comments and custom port

This commit is contained in:
ocueye2 2024-08-20 16:46:09 +00:00
parent 6453f47404
commit 0165c3cbdc

View File

@ -1,3 +1,5 @@
port = 8080
import os import os
import sys import sys
path = os.path.dirname(sys.argv[0]) path = os.path.dirname(sys.argv[0])
@ -7,10 +9,10 @@ css = os.listdir(f"{path}/css")
from http.server import SimpleHTTPRequestHandler, HTTPServer from http.server import SimpleHTTPRequestHandler, HTTPServer
# Custom handler class to serve "Hello, World!" at /test # main loop
class MyHandler(SimpleHTTPRequestHandler): class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self): def do_GET(self):
# Check if the request path is /test # makes index work
if self.path == "/": if self.path == "/":
selfpath = "index" selfpath = "index"
else: else:
@ -18,7 +20,7 @@ class MyHandler(SimpleHTTPRequestHandler):
paths = f"{selfpath}.html" paths = f"{selfpath}.html"
if paths.replace("/","") in html: if paths.replace("/","") in html:
# Respond with a 200 status code
@ -71,7 +73,8 @@ class MyHandler(SimpleHTTPRequestHandler):
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
self.end_headers() self.end_headers()
# The content of the webpage # Merge all the files together procedrealy
# todo: make compiler verson of this
content = f"""<html> content = f"""<html>
<style> <style>
{ccss} {ccss}
@ -95,9 +98,11 @@ class MyHandler(SimpleHTTPRequestHandler):
""" """
# Write the content to the response # send out html
self.wfile.write(content.encode()) self.wfile.write(content.encode())
else: else:
# 404 page
# todo: add custom 404 pages
print(paths.replace("/","")) print(paths.replace("/",""))
# If the # If the
# path is not /test, return a 404 Not Found response # path is not /test, return a 404 Not Found response
@ -106,11 +111,11 @@ class MyHandler(SimpleHTTPRequestHandler):
self.wfile.write(b"404 Not Found") self.wfile.write(b"404 Not Found")
# Set the server address and port # Set the server address and port
server_address = ('', 8080) server_address = ('', port)
# Create the HTTP server # Create the HTTP server
httpd = HTTPServer(server_address, MyHandler) httpd = HTTPServer(server_address, MyHandler)
# Serve the webpage # Serve the webpage
print("Serving on port 8080...") print(f"Serving on port {port}")
httpd.serve_forever() httpd.serve_forever()