Hello world 2 年之前
父節點
當前提交
b5c77d716b
共有 2 個文件被更改,包括 46 次插入25 次删除
  1. 8 0
      dockerfile
  2. 38 25
      laba4.py

+ 8 - 0
dockerfile

@@ -0,0 +1,8 @@
+from python3.10-slim
+
+RUN pip install --no-cache-dir --upgrade pip \
+  && pip install --no-cache-dir -r requests
+
+COPY laba4.py
+CMD ["python3","./laba4.py"]
+EXPOSE 8000

+ 38 - 25
laba4.py

@@ -1,45 +1,58 @@
-from http.server import HTTPServer, BaseHTTPRequestHandler
+from http.server \
+    import HTTPServer, BaseHTTPRequestHandler
+import requests
 import subprocess
+import datetime
 
-config = subprocess.check_output(
-    ['chcp', '65001', '&', 'netsh', 'interface', 'ipv4', 'show', 'config'], shell=True)
-mask = config.decode("utf-8").split('Ethernet')[1].split("\n")[3].split(":")[1].split(":")[0]
-ip = config.decode("utf-8").split('Ethernet')[1].split("\n")[2].split(":")[1]
-dns1 = config.decode("utf-8").split('Ethernet')[1].split("\n")[7].split(":")[1]
-dns2 = config.decode("utf-8").split('Ethernet')[1].split("\n")[8].split(":")[0]
-gateway = config.decode("utf-8").split('Ethernet')[1].split("\n")[4].split(":")[1]
-
-class HttpGethandler(BaseHTTPRequestHandler):
+class HttpGetHandler(BaseHTTPRequestHandler):
     def do_GET(self):
         try:
-            if self.path.endswith("/status"):
+            if self.path.endswith("/Status"):
                 self.send_response(200)
                 self.send_header("Content-type", "text/html")
                 self.end_headers()
-                html = " "
-                html += mask
-                html += ip
-                html += dns1
-                html += dns2
-                html += gateway
-                html = f"<html><head><meta charset='utf-8'><title>Damned HTTP-Server</title></head>" \
-                       f"<body>IP - {ip}<br>Mask - {mask}<br>Gateway - {gateway}<br>DNS 1 - {dns1}<br>DNS 2 - {dns2}</body>"
-                self.wfile.write(html.encode())
+                get = requests.get('https://api.ipify.org/')
+                http_text = f"<html><head><meta charset = 'utf-8'>" \
+                            f"<title>Простой HTTP-сервер.</title></head>" \
+                            f"<body>Ваш адресс {get.text}.<br>" \
+                            f"<body>'Enter new DNS1 address: x.x.x.x" \
+                            f"<form action=/post method=POST>" \
+                            f'<input type="text" id="dns_form1" name="dns1">' \
+                            f"</body></html>"
+
+                self.wfile.write(http_text.encode('utf-8'))
+
         except IOError:
-            self.send_error(400, f'Damned HTTP-Server not found{self.path}')
+            self.send_error(400, "File not found{self.path}")
+
+    def do_POST(self):
+        if self.path.endswith("/post"):
+            self.send_response(200)
+            self.send_header("Content-type", "text/html")
+            self.end_headers()
+            response = self.headers.get('Content-Length')
+            post_body = self.rfile.read(int(response)).decode('utf-8').split('=')[-1]
+            subprocess.call(f'netsh interface ipv4 set dns name="Ethernet" static {post_body} primary', shell=True)
+
+            http_text = f"<html><head><meta charset = 'utf-8'>" \
+                        f"<title>Ответ</title></head>" \
+                        f"<br>DNS адрес изменен" \
+                        f"</body></html>"
+
+            self.wfile.write(http_text.encode('utf-8'))
 
 
-def main(server_class=HTTPServer, handler_class=HttpGethandler):
+def main(server_class=HTTPServer, handler_class=HttpGetHandler):
     server_address = ('localhost', 8000)
     httpd = server_class(server_address, handler_class)
     try:
-        print("Damned HTTP-Server started")
+        print("Зеленый Свет")
         httpd.serve_forever()
 
     except KeyboardInterrupt:
         httpd.server_close()
-        print("Damned HTTP-Server stopped")
+        print("Красный свет")
 
 
 if __name__ == '__main__':
-    main()
+    main()