server-2.0.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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</h1>
  19. <p>General 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>Stalin Tyan</h1>
  34. <p>Slava Sovetskomu Souzu</p>
  35. <button onclick="location.href='/'">Back</button>
  36. <h2><img src="https://yt3.googleusercontent.com/ytc/AGIKgqODvXcSJJzpm-fzI3o42tAXsdA4AdX0j4EjuiNHlQE=s900-c-k-c0x00ffffff-no-rj"></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 = "Не найден :("
  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()