server.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import subprocess
  2. from http.server import BaseHTTPRequestHandler, HTTPServer
  3. serverPort = 8000
  4. config = subprocess.check_output(
  5. ['chcp', '65001', '&', 'netsh', 'interface', 'ipv4', 'show', 'config'], shell=True)
  6. ipv4_ip = \
  7. config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[2].split(' ')[-1]
  8. ipv4_mask = \
  9. config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[3].split(' ')[-1][:-1][:-1]
  10. ipv4_gateway = \
  11. config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[4].split(' ')[-1]
  12. ipv4_dns_1 = \
  13. config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[7].split(' ')[-1]
  14. ipv4_dns_2 = \
  15. config.decode('utf-8').split('Configuration for interface "Ethernet"')[1].split('\n')[8].split(' ')[-1]
  16. class HttpGetHandler(BaseHTTPRequestHandler):
  17. def do_GET(self):
  18. if self.path.endswith("/"):
  19. self.send_response(200)
  20. self.send_header("Content-type", "text/html")
  21. self.end_headers()
  22. html = f"<!doctype html><html><head><meta charset=utf-8><title>HTTP server</title></head><body>" \
  23. f"IP Address: {ipv4_ip}<br>" \
  24. f"Mask: {ipv4_mask}<br>" \
  25. f"Default gateway: {ipv4_gateway}<br>" \
  26. f"DNS1: {ipv4_dns_1}<br>" \
  27. f"DNS2: {ipv4_dns_2}<br>" \
  28. f"</body></html>"
  29. self.wfile.write(html.encode('utf-8'))
  30. def main(server_class=HTTPServer, handler_class=HttpGetHandler):
  31. server_address = ('localhost', 8000)
  32. httpd = server_class(server_address, handler_class)
  33. try:
  34. print("Server run...")
  35. httpd.serve_forever()
  36. except KeyboardInterrupt:
  37. httpd.server_close()
  38. print("Stopped.")
  39. if __name__ == '__main__':
  40. main()