main.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import HTTPServer, BaseHTTPRequestHandler
  2. import requests
  3. import subprocess
  4. import datetime
  5. class HttpGetHandler(BaseHTTPRequestHandler):
  6. def do_GET(self):
  7. try:
  8. if self.path.endswith("/Status"):
  9. self.send_response(200)
  10. self.send_header("Content-type", "text/html")
  11. self.end_headers()
  12. get = requests.get('https://api.ipify.org/')
  13. http_text = f"<html><head><meta charset = 'utf-8'>" \
  14. f"<title>Main.</title></head>" \
  15. f"<body>Your address {get.text}.<br>" \
  16. f"<body>'Enter new DNS1 address: x.x.x.x" \
  17. f"<form action=/post method=POST>" \
  18. f'<input type="text" id="dns_form1" name="dns1">' \
  19. f"</body></html>"
  20. self.wfile.write(http_text.encode('utf-8'))
  21. except IOError:
  22. self.send_error(400, "File not found{self.path}")
  23. def do_POST(self):
  24. if self.path.endswith("/post"):
  25. self.send_response(200)
  26. self.send_header("Content-type", "text/html")
  27. self.end_headers()
  28. response = self.headers.get('Content-Length')
  29. post_body = self.rfile.read(int(response)).decode('utf-8').split('=')[-1]
  30. subprocess.call(f'netsh interface ipv4 set dns name="Ethernet" static {post_body} primary', shell=True)
  31. http_text = f"<html><head><meta charset = 'utf-8'>" \
  32. f"<title>Answer</title></head>" \
  33. f"<br>DNS Change" \
  34. f"</body></html>"
  35. self.wfile.write(http_text.encode('utf-8'))
  36. def main(server_class=HTTPServer, handler_class=HttpGetHandler):
  37. server_address = ('localhost', 8000)
  38. httpd = server_class(server_address, handler_class)
  39. try:
  40. print("Start")
  41. httpd.serve_forever()
  42. except KeyboardInterrupt:
  43. httpd.server_close()
  44. print("Stop")
  45. if __name__ == '__main__':
  46. main()