Lösungen der Aufgaben vom Kurs MEIM1 - Web Engineering von Prof. Hofmann
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http-server 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #/usr/bin/env python3
  2. from http import server
  3. from urllib import parse
  4. class SimpleHandler(server.BaseHTTPRequestHandler):
  5. def do_GET(self):
  6. self.send_response(200)
  7. self.end_headers()
  8. parsed = parse.urlparse(self.path)
  9. path = parsed.path
  10. query = parsed.query
  11. query_components = parse.parse_qsl(query)
  12. if path == "/":
  13. self.return_get_components(path, query, query_components)
  14. elif path == "/ying":
  15. self.return_ying()
  16. elif path == "/yang":
  17. self.return_yang()
  18. elif path == "/squares":
  19. self.return_squares(query_components)
  20. else:
  21. msg = "Error 404"
  22. self.wfile.write(msg.encode('utf-8'))
  23. def return_squares(self, query_components):
  24. if len(query_components) != 2:
  25. msg = "Invalid number of Query Components! Two components needed."
  26. else:
  27. if query_components[0][0] != 'von' or query_components[1][0] != 'bis':
  28. msg = "Invalid Query Components! First key has to be 'von', second key 'bis'."
  29. self.wfile.write(msg.encode('utf-8'))
  30. else:
  31. try:
  32. von = int(query_components[0][1])
  33. bis = int(query_components[1][1])
  34. except:
  35. msg = "One or both query values are not integer."
  36. else:
  37. msg = "<table border='1'>"
  38. for i in range(von, bis+1):
  39. msg += f"<tr><td>{i}</td><td>{i**2}</td></tr>"
  40. msg += "</table>"
  41. self.wfile.write(msg.encode('utf-8'))
  42. def return_ying(self):
  43. msg = "<a href='/yang'>Ying</a>"
  44. self.wfile.write(msg.encode('utf-8'))
  45. def return_yang(self):
  46. msg = "<a href='/ying'>Yang</a>"
  47. self.wfile.write(msg.encode('utf-8'))
  48. def return_get_components(self, path, query, query_components):
  49. msg = f"Path: {path}\nQuery: {query}\nComponents: {str(query_components)}"
  50. self.wfile.write(msg.encode('utf-8'))
  51. if __name__ == "__main__":
  52. port = 60000
  53. handler = SimpleHandler
  54. address = ('', port)
  55. server = server.HTTPServer(address, handler)
  56. server.serve_forever()