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.1KB

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