Code.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from http.server import HTTPServer, BaseHTTPRequestHandler
  2. from datetime import datetime
  3. import re
  4. class HttpGetHandler(BaseHTTPRequestHandler):
  5. def do_GET(self):
  6. try:
  7. if self.path.endswith("/"):
  8. self.send_response(200)
  9. self.send_header("Content-type", "text/html")
  10. self.end_headers()
  11. http_text = """<html><head><meta charset="utf-8">
  12. <title>Simple HTTP Server</title></head>
  13. <body>Главная страница<br><br>
  14. <a href="http://localhost:8000/info">Info</a><br>
  15. <a href="http://localhost:8000/status">Статус</a></body><html>"""
  16. self.wfile.write(http_text.encode())
  17. if self.path.endswith("/info"):
  18. self.send_response(200)
  19. self.send_header("Content-type", "text/html")
  20. self.end_headers()
  21. http_text = """<html><head><meta charset="utf-8">
  22. <title>Info</title></head>
  23. <body>Кипиченков Никита Вячеславович 701(3)<br><br>
  24. <a href="http://localhost:8000/">На главную</a><br>
  25. <a href="http://localhost:8000/status">Статус</a></body><html>"""
  26. self.wfile.write(http_text.encode())
  27. if self.path.endswith("/status"):
  28. self.send_response(200)
  29. self.send_header("Content-type", "text/html")
  30. self.end_headers()
  31. dt = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
  32. _ip = self.client_address[0]
  33. _ip = re.sub("([.][0-9]{1,3})", ".x", _ip, count = 2)
  34. http_text = f"<html><head><meta charset='utf-8'>" \
  35. f"<title>Status</title></head>" \
  36. f"<body>Ваш IP:{_ip}<br>" \
  37. f"ФИО: Кипиченков Никита Вячеславович<br>" \
  38. f"Дата: {dt}<br><br>" \
  39. f"<a href='http://localhost:8000/'>На главную</a><br>" \
  40. f"<a href='http://localhost:8000/info'>Info</a></body></html>"
  41. self.wfile.write(http_text.encode())
  42. except IOError:
  43. self.send_error(400,f"File not found{self.path}")
  44. def main(server_class=HTTPServer,handler_class=HttpGetHandler):
  45. server_address = ('localhost',8000)
  46. httpd = server_class(server_address,handler_class)
  47. try:
  48. print("Starting the Server!")
  49. httpd.serve_forever()
  50. except KeyboardInterrupt:
  51. httpd.server_close()
  52. print("Killing the Server!")
  53. if __name__ == "__main__":
  54. main()