mako 2 роки тому
коміт
d9f712fed9
3 змінених файлів з 74 додано та 0 видалено
  1. 7 0
      dockerfile
  2. 66 0
      python.py
  3. 1 0
      requirements.txt

+ 7 - 0
dockerfile

@@ -0,0 +1,7 @@
+FROM python:3.9.16
+WORKDIR /docker
+COPY requirements.txt requirements.txt
+RUN pip3 install -r requirements.txt
+COPY . .
+CMD ["python3","./python.py"]
+EXPOSE 8000

+ 66 - 0
python.py

@@ -0,0 +1,66 @@
+from http.server import HTTPServer, BaseHTTPRequestHandler
+import requests
+import re
+from datetime import datetime
+class HttpGetHandler(BaseHTTPRequestHandler):
+    def do_GET(self):
+        try:
+            if self.path.endswith("/"):
+                self.send_response(200)
+
+                self.send_header("Content-type", "text/html")
+                self.end_headers()
+
+                http_text = """<html><head><meta charset='utf-8'>
+                                <title>Simple HTTP Server</title></head>
+                                <body>Ok<br>
+                                <a href="http://localhost:8000/info">Info</a></body><html>
+                                <a href="http://localhost:8000/Status">Status</a></body><html>"""
+               
+                self.wfile.write(http_text.encode())
+            if self.path.endswith("/info"):
+                self.send_response(200)
+
+                self.send_header("Content-type", "text/html")
+                self.end_headers()
+
+                http_text = """<html><head><meta charset='utf-8'>
+                                <title>Info</title></head>
+                                <body>Info<br>
+                                <body>Kapitanyuk Alexey Konstantinovich<br>
+                                <body>gr-701(3)<br>
+                                <a href="http://localhost:8000/">Ok</a></body><html>
+                                <a href="http://localhost:8000/Status">Status</a></body><html>"""
+               
+                self.wfile.write(http_text.encode())
+            if self.path.endswith("/Status"):
+                self.send_response(200)
+
+                self.send_header("Content-type", "text/html")
+                self.end_headers()
+                pattern = r'\d{2,}\.\d{2,}\.\d{2,}\.\d{2,}'
+                ip = re.search(pattern, html).group()
+                dt = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")
+                http_text = f"Status" \
+                            f"FIO: Kapitanyuk Alexey Konstantinovich<br>" \
+                            f"Date and time: {dt}<br><br>" \
+                            f"Ip-address: {ip} <br>" \
+                            f"<a href='http://localhost:8000/'>Ok</a><br>" \
+                            f"<a href='http://localhost:8000/info'>Info</a></body></html>"       
+                self.wfile.write(http_text.encode())
+        except IOError:
+            self.send_error(400,f"File not found{self.path}")
+
+def main(server_class=HTTPServer,handler_class=HttpGetHandler):
+    server_address = ('localhost',8000)
+    httpd = server_class(server_address,handler_class)
+    try: 
+        print("Start server")
+        httpd.serve_forever()
+
+    except KeyboardInterrupt:
+        httpd.server_close()
+        print("Kill server")
+
+if __name__ == '__main__':
+    main()

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+datetime