|
@@ -1,5 +1,7 @@
|
|
|
+import datetime
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
|
|
|
|
+import requests
|
|
|
class HttpGethandler(BaseHTTPRequestHandler):
|
|
|
def do_GET(self):
|
|
|
try:
|
|
@@ -9,11 +11,11 @@ class HttpGethandler(BaseHTTPRequestHandler):
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
self.end_headers()
|
|
|
|
|
|
- http_text = """<html><head><meta charset='utf-8'
|
|
|
- <title>Простой HTTP-сервер</title></head>
|
|
|
- <body>Это главная страница.</body></html>"""
|
|
|
+ html = f"<html><head><meta charset='utf-8'><title>HTTP-Server</title></head>" \
|
|
|
+ f"<body>HTTP-Server working" \
|
|
|
+ f"</body></html>"
|
|
|
|
|
|
- self.wfile.write(http_text.encode())
|
|
|
+ self.wfile.write(html.encode())
|
|
|
|
|
|
if self.path.endswith("/info"):
|
|
|
self.send_response(200)
|
|
@@ -21,25 +23,39 @@ class HttpGethandler(BaseHTTPRequestHandler):
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
self.end_headers()
|
|
|
|
|
|
- http_text = """<html><head><meta charset='utf-8'
|
|
|
- <title>Ондар А.В. 701(3)</title></head></html>"""
|
|
|
+ html = f"<html><head><meta charset='utf-8'><title>HTTP-Server</title></head>" \
|
|
|
+ f"<body>Ondar Ajhaan 701(3)" \
|
|
|
+ f"</body></html>"
|
|
|
|
|
|
- self.wfile.write(http_text.encode())
|
|
|
+ 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/')
|
|
|
+
|
|
|
+ html = f"<html><head><meta charset='utf-8'><title>Damned HTTP-Server</title></head>" \
|
|
|
+ f"<body>Your IP address on the HTTP-Server: {get.text}<br>" \
|
|
|
+ f"Ondar A.V. 701(3)<br>" \
|
|
|
+ f"Time on the HTTP-Server: {str(datetime.datetime.now())}" \
|
|
|
+ f"</body></html>"
|
|
|
+ self.wfile.write(html.encode())
|
|
|
|
|
|
except IOError:
|
|
|
- self.send_error(400, f"Not found{self.path}")
|
|
|
+ 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("Start server!")
|
|
|
+ print("HTTP-Server started!")
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
httpd.server_close()
|
|
|
- print("Server stop!")
|
|
|
+ print("HTTP-Server stopped!")
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|