server-2.0(1).py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import http.server
  2. import socketserver
  3. import socket
  4. from datetime import datetime
  5. from ipaddress import ip_address
  6. import self
  7. from pip._internal.vcs import git
  8. class MyHandler(http.server.SimpleHTTPRequestHandler):
  9. def do_GET(self):
  10. if self.path == '/':
  11. self.send_response(200)
  12. self.send_header('Content-type', 'text/html; charset=utf-8')
  13. self.end_headers()
  14. self.wfile.write(b"""
  15. <html>
  16. <head><title>Fits Page</title></head>
  17. <body>
  18. <h1>Hello world!</h1>
  19. <p>Main page</p>
  20. <button onclick="location.href='/status'">Status</button>
  21. <button onclick="location.href='/img'">img</button>
  22. </body>
  23. </html>
  24. """)
  25. elif self.path == '/img':
  26. self.send_response(200)
  27. self.send_header('Content-type', 'text/html; charset=utf-8')
  28. self.end_headers()
  29. self.wfile.write(b"""
  30. <html>
  31. <head><title>Image</title></head>
  32. <body>
  33. <h1>Kotik</h1>
  34. <p>Valera</p>
  35. <button onclick="location.href='/'">Back</button>
  36. <h2><img src="https://sun9-5.userapi.com/impg/Wh22NP0d40s2qlcKFYV6-KdfGZjS5IfsCKrzEQ/z_j4vlD3lDs.jpg?size=512x512&quality=96&sign=479f6575d6e4a0f355f1f039c7564fa7&type=album"></h2>
  37. </body>
  38. </html>
  39. """)
  40. elif self.path == '/status':
  41. ip_address = self.client_address[0]
  42. ip_parts = ip_address[0].split('.')
  43. if len(ip_parts) == 4:
  44. masked_ip = f"{ip_parts[0]}.***.***.{ip_parts[3]}"
  45. else:
  46. masked_ip = "Not Found"
  47. student_name = ("Виноградов_Дмитрий_Владимирович_714_группа")
  48. current_time = datetime.now().strftime("%d.%m.%Y %I:%M:%S %p")
  49. response_content = f"""
  50. <html>
  51. <head><title>Status Page</title></head>
  52. <body>
  53. <h1>Статус:</h1>
  54. <p>Ip-адрес: {masked_ip}</p>
  55. <p>Фио: {student_name}</p>
  56. <p>Текущее время: {current_time}</p>
  57. <button onclick="location.href='/'">Back</button>
  58. </body>
  59. </html>
  60. """.encode("utf-8")
  61. self.send_response(200)
  62. self.send_header('Content-type', 'text/html; charset=utf-8')
  63. self.end_headers()
  64. self.wfile.write(response_content)
  65. hostname = socket.gethostname()
  66. port = 8080
  67. with socketserver.TCPServer((hostname, port), MyHandler) as httpd:
  68. print(f"Server on {hostname}:{port}")
  69. httpd.serve_forever()