main.py 2.2 KB

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