|
@@ -1,43 +1,51 @@
|
|
|
+import subprocess
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
-import requests
|
|
|
-from datetime import datetime
|
|
|
|
|
|
serverPort = 8000
|
|
|
|
|
|
-class Server(BaseHTTPRequestHandler):
|
|
|
+config = subprocess.check_output(
|
|
|
+ ['chcp', '65001', '&', 'netsh', 'interface', 'ipv4', 'show', 'config'], shell=True)
|
|
|
+ipv4_ip = \
|
|
|
+ config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[2].split(' ')[-1]
|
|
|
+ipv4_mask = \
|
|
|
+ config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[3].split(' ')[-1][:-1][:-1]
|
|
|
+ipv4_gateway = \
|
|
|
+ config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[4].split(' ')[-1]
|
|
|
+ipv4_dns_1 = \
|
|
|
+ config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[7].split(' ')[-1]
|
|
|
+ipv4_dns_2 = \
|
|
|
+ config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[8].split(' ')[-1]
|
|
|
+
|
|
|
+
|
|
|
+class HttpGetHandler(BaseHTTPRequestHandler):
|
|
|
|
|
|
def do_GET(self):
|
|
|
if self.path.endswith("/"):
|
|
|
self.send_response(200)
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
self.end_headers()
|
|
|
- self.wfile.write("200_OK".encode("utf-8"))
|
|
|
+ html = f"<!doctype html><html><head><meta charset=utf-8><title>HTTP server</title></head><body>" \
|
|
|
+ f"IP Address: {ipv4_ip}<br>" \
|
|
|
+ f"Mask: {ipv4_mask}<br>" \
|
|
|
+ f"Default gateway: {ipv4_gateway}<br>" \
|
|
|
+ f"DNS1: {ipv4_dns_1}<br>" \
|
|
|
+ f"DNS2: {ipv4_dns_2}<br>" \
|
|
|
+ f"</body></html>"
|
|
|
|
|
|
+ self.wfile.write(html.encode('utf-8'))
|
|
|
|
|
|
- if self.path.endswith("/info"):
|
|
|
- self.send_response(200)
|
|
|
- self.send_header("Content-type", "text/html")
|
|
|
- self.end_headers()
|
|
|
- self.wfile.write("Alexey Shumkov 703".encode("utf-8"))
|
|
|
-
|
|
|
- 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"<!doctype html><html><head><meta charset=utf-8><title>HTTP server</title></head><body>"\
|
|
|
- f" Your ip address: {get.text}<br>"\
|
|
|
- f"Time on server: {datetime.now()}"\
|
|
|
- f"</body></html>"
|
|
|
+def main(server_class=HTTPServer, handler_class=HttpGetHandler):
|
|
|
+ server_address = ('localhost', 8000)
|
|
|
+ httpd = server_class(server_address, handler_class)
|
|
|
+ try:
|
|
|
+ print("Server run...")
|
|
|
+ httpd.serve_forever()
|
|
|
|
|
|
- self.wfile.write(html.encode(encoding='utf-8'))
|
|
|
+ except KeyboardInterrupt:
|
|
|
+ httpd.server_close()
|
|
|
+ print("Stopped.")
|
|
|
|
|
|
|
|
|
-server = HTTPServer(("", serverPort), Server)
|
|
|
-print(f"Server started at http://localhost:{serverPort}")
|
|
|
-try:
|
|
|
- server.serve_forever()
|
|
|
-except KeyboardInterrupt:
|
|
|
- server.server_close()
|
|
|
- print("Server stopped.")
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|