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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. msg = self.get_components(path, query, query_components)
  14. elif path == "/ying":
  15. msg = self.get_ying()
  16. elif path == "/yang":
  17. msg = self.get_yang()
  18. elif path == "/squares":
  19. msg = self.get_squares(query_components)
  20. else:
  21. msg = "Error 404"
  22. self.wfile.write(msg.encode('utf-8'))
  23. def get_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. return msg
  42. def get_ying(self):
  43. msg = "<a href='/yang'>Ying</a>"
  44. return msg
  45. def get_yang(self):
  46. msg = "<a href='/ying'>Yang</a>"
  47. return msg
  48. def get_components(self, path, query, query_components):
  49. msg = f"Path: {path}\nQuery: {query}\nComponents: {str(query_components)}"
  50. return msg
  51. if __name__ == "__main__":
  52. port = 60000
  53. handler = SimpleHandler
  54. address = ('', port)
  55. server = server.HTTPServer(address, handler)
  56. server.serve_forever()