import http.server
import urllib.parse

class MyOwnHandler(http.server.BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        parsed = urllib.parse.urlparse(self.path)

        path = parsed.path
        query = parsed.query
        query_components = urllib.parse.parse_qsl(query)

        # query_components has form: [ ('von_param','???') ,  ('bis_param','???') ]

        print("Die Query lautet: ", query_components)

        if query_components != "[]":
            von_param = int(query_components[0][1])
            bis_param = int(query_components[1][1])

            values = []

            for curr_number in range(von_param, bis_param +1):
                values.append((curr_number, curr_number**2),)

            msg_to_return = "<html><head><title>Wertetabelle_mit_Quadratzahlen</title></head>"
            msg_to_return += "<body><table>Quadrat_Tabelle"
            msg_to_return += "<tr><th>QUADRATZAHLEN</th></tr>"

            for all_tuples in values:
                msg_to_return += f"<tr><td>{all_tuples[0]}</td><td>{all_tuples[1]}</td></tr>"

            msg_to_return += "</table></body>"

            self.wfile.write(msg_to_return.encode("utf-8"))
        else:
            pass


port = 33444
handler = MyOwnHandler
address = ('', port)

myserver = http.server.HTTPServer(address, handler)
myserver.serve_forever()