2к.py 2.9 KB

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