Browse Source

laba2 with Status code

gr712_shkvan 6 months ago
parent
commit
ad8caa12ea
1 changed files with 49 additions and 0 deletions
  1. 49 0
      laba2_status.md

+ 49 - 0
laba2_status.md

@@ -0,0 +1,49 @@
+import http.server
+from datetime import datetime
+import socket
+
+class MyHandler(http.server.SimpleHTTPRequestHandler):
+    def do_GET(self):
+        if self.path == '/status':
+            self.send_response(200)
+            self.send_header('Content-type', 'text/html; charset=utf-8')
+            self.end_headers()
+            self.wfile.write(self.generate_status_page().encode('utf-8'))
+        else:
+            self.send_error(404, "Page not found")
+
+    def generate_status_page(self):
+        # Получение IP адреса
+        ip_address = self.client_address[0]
+        # Скрытие 2 и 3 октетов IP адреса
+        ip_parts = ip_address.split('.')
+        masked_ip = f"{ip_parts[0]}.***.***.{ip_parts[3]}"
+        
+        # ФИО
+        full_name = "Шкляров Виктор Андреевич"
+        
+        # Текущее время
+        current_time = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
+        
+        # Формирование HTML страницы
+        html = f"""
+        <html>
+        <head><title>Status</title></head>
+        <body>
+            <h1>Status Page</h1>
+            <p>IP-адрес: {masked_ip}</p>
+            <p>ФИО: {full_name}</p>
+            <p>Текущее время: {current_time}</p>
+        </body>
+        </html>
+        """
+        return html
+
+def run(server_class=http.server.HTTPServer, handler_class=MyHandler):
+    server_address = ('', 8081)
+    httpd = server_class(server_address, handler_class)
+    print("Starting server on port 8081...")
+    httpd.serve_forever()
+
+if __name__ == '__main__':
+    run()