|
@@ -0,0 +1,59 @@
|
|
|
+import datetime
|
|
|
+from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
|
+
|
|
|
+import requests
|
|
|
+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()
|
|
|
+
|
|
|
+ html = f"<html><head><meta charset='utf-8'><title>HTTP-Server</title></head>" \
|
|
|
+ f"<body>HTTP-Server working" \
|
|
|
+ f"</body></html>"
|
|
|
+
|
|
|
+ self.wfile.write(html.encode())
|
|
|
+
|
|
|
+ if self.path.endswith("/info"):
|
|
|
+ self.send_response(200)
|
|
|
+
|
|
|
+ self.send_header("Content-type", "text/html")
|
|
|
+ self.end_headers()
|
|
|
+
|
|
|
+ html = f"<html><head><meta charset='utf-8'><title>HTTP-Server</title></head>" \
|
|
|
+ f"<body>Ондар Ай-Хаан 701(3)" \
|
|
|
+ f"</body></html>"
|
|
|
+
|
|
|
+ self.wfile.write(html.encode())
|
|
|
+
|
|
|
+ if self.path.endswith("/status"):
|
|
|
+ self.send_response(200)
|
|
|
+ self.send_header("Content-type", "text/html")
|
|
|
+ self.end_headers()
|
|
|
+ get = requests.get('https://api.ipify.org/')
|
|
|
+
|
|
|
+ http_text = f' <!doctype html><html><head><meta charset= utf-8><body>IP-адрес: {get.text}<br>Время: {datetime.now()}</body></html>'
|
|
|
+
|
|
|
+ self.wfile.write(html.encode())
|
|
|
+
|
|
|
+ except IOError:
|
|
|
+ self.send_error(400, f"HTTP-Server no found{self.path}")
|
|
|
+
|
|
|
+
|
|
|
+def main(server_class=HTTPServer, handler_class=HttpGethandler):
|
|
|
+ server_address = ('localhost', 8000)
|
|
|
+ httpd = server_class(server_address, handler_class)
|
|
|
+ try:
|
|
|
+ print("HTTP-Server started!")
|
|
|
+ httpd.serve_forever()
|
|
|
+
|
|
|
+ except KeyboardInterrupt:
|
|
|
+ httpd.server_close()
|
|
|
+ print("HTTP-Server stopped!")
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|