"""
Runs the web server
"""
import sys
import socketserver
from http.server import SimpleHTTPRequestHandler
import os
import mimetypes
import time


class ServerHandler(SimpleHTTPRequestHandler):
    """
    Gets resources and their mimetype and
    loads the page correctly if found, else gives error 404
    """

    def do_GET(self):
        try:
            if self.path in ("", "/"):
                filepath = "html/index.html"
            else:
                filepath = self.path.lstrip("/")

            f = open(os.path.join('.', filepath), "rb")

        except IOError:
            self.send_error(404, f"File Not Found: {self.path}")

        else:
            self.send_response(200)

            # mimetypes handled automatically.
            mimetype, _ = mimetypes.guess_type(filepath)
            self.send_header("Content-type", mimetype)
            self.end_headers()
            for request in f:
                self.wfile.write(request)


def main():
    """
    First called method when file is run. Runs the server
    """
    # asks continuously the user a non-privileged port number [1024-65535] till
    # one is given; pressing, CTRL+C will quit
    while True:
        try:
            port = int(input("Choose a port number[1024-65535]: "))
        except KeyboardInterrupt:
            print("\nQuitting...")
            time.sleep(0.5)
            print("Quit successfully")
            sys.exit(0)
        except ValueError:
            print("Not a valid port number")
            continue

        # privileged port numbers are from 0 to 1023
        if port not in range(1024, 65535+1):
            print("This is a privileged port number. Try again...")
            continue
        break

    server = socketserver.ThreadingTCPServer(("", port), ServerHandler)
    print(f"Server running on port {port}...")
    print(f"Open your browser and head to 'localhost:{port}/'")
    try:
        # quit using CTRL + C
        server.serve_forever()
    except KeyboardInterrupt:
        print("The server quit successfully...")
    server.server_close()
    sys.exit(0)


if __name__ == "__main__":
    main()

"""
Runs the web server
"""
import sys
import socketserver
from http.server import SimpleHTTPRequestHandler
import os
import mimetypes
import time


class ServerHandler(SimpleHTTPRequestHandler):
    """
    Gets resources and their mimetype and
    loads the page correctly if found, else gives error 404
    """

    def do_GET(self):
        try:
            if self.path in ("", "/"):
                filepath = "html/index.html"
            else:
                filepath = self.path.lstrip("/")

            f = open(os.path.join('.', filepath), "rb")

        except IOError:
            self.send_error(404, f"File Not Found: {self.path}")

        else:
            self.send_response(200)

            # mimetypes handled automatically.
            mimetype, _ = mimetypes.guess_type(filepath)
            self.send_header("Content-type", mimetype)
            self.end_headers()
            for request in f:
                self.wfile.write(request)


def main():
    """
    First called method when file is run. Runs the server
    """
    # asks continuously the user a non-privileged port number [1024-65535] till
    # one is given; pressing, CTRL+C will quit
    while True:
        try:
            port = int(input("Choose a port number[1024-65535]: "))
        except KeyboardInterrupt:
            print("\nQuitting...")
            time.sleep(0.5)
            print("Quit successfully")
            sys.exit(0)
        except ValueError:
            print("Not a valid port number")
            continue

        # privileged port numbers are from 0 to 1023
        if port not in range(1024, 65535+1):
            print("This is a privileged port number. Try again...")
            continue
        break

    server = socketserver.ThreadingTCPServer(("", port), ServerHandler)
    print(f"Server running on port {port}...")
    print(f"Open your browser and head to 'localhost:{port}/'")
    try:
        # quit using CTRL + C
        server.serve_forever()
    except KeyboardInterrupt:
        print("The server quit successfully...")
    server.server_close()
    sys.exit(0)


if __name__ == "__main__":
    main()

Runs the web server Gets resources and their mimetype and     loads the page correctly if found, else gives error 404 # mimetypes handled automatically. First called method when file is run. Runs the server # asks continuously the user a non-privileged port number [1024-65535] till # one is given; pressing, CTRL+C will quit # privileged port numbers are from 0 to 1023 # quit using CTRL + C