|
|
|
|
|
|
|
|
|
|
|
#/usr/bin/env python3 |
|
|
|
|
|
|
|
|
|
|
|
from http import server |
|
|
|
|
|
from urllib import parse |
|
|
|
|
|
|
|
|
|
|
|
class SimpleHandler(server.BaseHTTPRequestHandler): |
|
|
|
|
|
|
|
|
|
|
|
def do_GET(self): |
|
|
|
|
|
self.send_response(200) |
|
|
|
|
|
self.end_headers() |
|
|
|
|
|
|
|
|
|
|
|
parsed = parse.urlparse(self.path) |
|
|
|
|
|
path = parsed.path |
|
|
|
|
|
query = parsed.query |
|
|
|
|
|
query_components = parse.parse_qsl(query) |
|
|
|
|
|
|
|
|
|
|
|
if path == "/": |
|
|
|
|
|
self.return_get_components(path, query, query_components) |
|
|
|
|
|
elif path == "/ying": |
|
|
|
|
|
self.return_ying() |
|
|
|
|
|
elif path == "/yang": |
|
|
|
|
|
self.return_yang() |
|
|
|
|
|
elif path == "/squares": |
|
|
|
|
|
self.return_squares(query_components) |
|
|
|
|
|
else: |
|
|
|
|
|
msg = "Error 404" |
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
def return_squares(self, query_components): |
|
|
|
|
|
if len(query_components) != 2: |
|
|
|
|
|
msg = "Invalid number of Query Components! Two components needed." |
|
|
|
|
|
else: |
|
|
|
|
|
if query_components[0][0] != 'von' or query_components[1][0] != 'bis': |
|
|
|
|
|
msg = "Invalid Query Components! First key has to be 'von', second key 'bis'." |
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
else: |
|
|
|
|
|
try: |
|
|
|
|
|
von = int(query_components[0][1]) |
|
|
|
|
|
bis = int(query_components[1][1]) |
|
|
|
|
|
except: |
|
|
|
|
|
msg = "One or both query values are not integer." |
|
|
|
|
|
else: |
|
|
|
|
|
msg = "<table border='1'>" |
|
|
|
|
|
for i in range(von, bis+1): |
|
|
|
|
|
msg += f"<tr><td>{i}</td><td>{i**2}</td></tr>" |
|
|
|
|
|
msg += "</table>" |
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def return_ying(self): |
|
|
|
|
|
msg = "<a href='/yang'>Ying</a>" |
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
def return_yang(self): |
|
|
|
|
|
msg = "<a href='/ying'>Yang</a>" |
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
def return_get_components(self, path, query, query_components): |
|
|
|
|
|
msg = f"Path: {path}\nQuery: {query}\nComponents: {str(query_components)}" |
|
|
|
|
|
|
|
|
|
|
|
self.wfile.write(msg.encode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
port = 60000 |
|
|
|
|
|
handler = SimpleHandler |
|
|
|
|
|
address = ('', port) |
|
|
|
|
|
|
|
|
|
|
|
server = server.HTTPServer(address, handler) |
|
|
|
|
|
server.serve_forever() |