Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from http.server import BaseHTTPRequestHandler, HTTPServer
- import urllib
- # Estado dos LEDs por ID
- esp_led_states = {}
- class SimpleHandler(BaseHTTPRequestHandler):
- # Armazena a última imagem recebida
- last_image = None
- def do_GET(self):
- parsed_path = urllib.parse.urlparse(self.path)
- path = parsed_path.path
- query_components = urllib.parse.parse_qs(parsed_path.query)
- esp_id = query_components.get("id", [None])[0]
- #global esp_led_states
- # agora você pode acessar/modificar esp_led_states
- if path == "/led_status":
- if not esp_id:
- self.send_error(400, "ID do ESP ausente na query string (?id=ESP_ID)")
- return
- # Verificando se a requisição é para alternar o LED
- toggle = query_components.get("toggle", [None])[0] # 'toggle' vai alternar o estado
- if toggle == "true":
- current_state = esp_led_states.get(esp_id, False) # Padrão para False se ID não conhecido
- new_state = not current_state # Alterna o estado
- esp_led_states[esp_id] = new_state # Atualiza o estado do LED
- print(f"[GET /led_status] ID: {esp_id}, Novo estado do LED: {new_state}")
- response_message = "LED acionado" if new_state else "LED desligado"
- else:
- current_state = esp_led_states.get(esp_id, False) # Padrão para False se ID não conhecido
- response_message = "True" if current_state else "False"
- self.send_response(200)
- self.send_header("Content-Type", "text/plain")
- self.end_headers()
- self.wfile.write(response_message.encode()) # Converte para bytes
- print(f"[GET /led_status] ID: {esp_id}, Estado enviado: {response_message}")
- elif path == "/photos/latest":
- if SimpleHandler.last_image:
- self.send_response(200)
- self.send_header("Content-Type", "image/jpeg")
- self.end_headers()
- self.wfile.write(SimpleHandler.last_image)
- else:
- self.send_error(404, "Imagem não encontrada")
- elif path == "/":
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- html_content = """
- <html>
- <head>
- <title>Servidor ESP32-CAM</title>
- <script>
- function toggleLed(espId) {
- fetch(`/toggle_led?id=${espId}`, { method: "POST" })
- .then(() => {
- fetch(`/led_status?id=${espId}`)
- .then(response => response.text())
- .then(state => {
- const newState = (state === "True") ? "ON" : "OFF";
- document.getElementById("led-state").innerText = newState;
- });
- })
- .catch(error => alert("Erro ao alternar o LED"));
- }
- function fetchLatestPhoto(espId) {
- const timestamp = Date.now(); // Para evitar cache
- const imageUrl = `/photos/latest?id=${espId}&nocache=${timestamp}`;
- document.getElementById("latest-photo").src = imageUrl;
- }
- setInterval(function () {
- fetchLatestPhoto("esp01");
- }, 5000);
- </script>
- </head>
- <body>
- <h1>Servidor Python para ESP32-CAM Ativo</h1>
- <p>Estado do LED (ID: ESP1): <span id="led-state">OFF</span></p>
- <button onclick="toggleLed('esp01')">Alternar LED</button>
- <h2>Última Foto</h2>
- <img id="latest-photo" src="" alt="Última foto do ESP" width="300"/>
- </body>
- </html>
- """
- self.wfile.write(html_content.encode())
- else:
- self.send_error(404, f"Endpoint GET não encontrado: {path}")
- def do_POST(self):
- parsed_path = urllib.parse.urlparse(self.path)
- path = parsed_path.path
- query_components = urllib.parse.parse_qs(parsed_path.query)
- esp_id = query_components.get("id", [None])[0]
- if path == "/upload_image":
- content_length = int(self.headers['Content-Length'])
- post_data = self.rfile.read(content_length)
- SimpleHandler.last_image = post_data
- self.send_response(200)
- self.send_header("Content-Type", "text/plain")
- self.end_headers()
- self.wfile.write(b"Imagem recebida com sucesso")
- print(f"[POST /upload_image] Imagem recebida, tamanho: {len(post_data)} bytes")
- elif path == "/toggle_led":
- if not esp_id:
- self.send_error(400, "ID do ESP ausente")
- return
- current = esp_led_states.get(esp_id, False)
- esp_led_states[esp_id] = not current
- print(f"[POST /toggle_led] ID: {esp_id}, Novo estado: {esp_led_states[esp_id]}")
- self.send_response(200)
- self.send_header("Content-Type", "text/plain")
- self.end_headers()
- self.wfile.write(b"OK")
- else:
- self.send_error(404, f"Endpoint POST não encontrado: {path}")
- # Configuração do servidor
- def run(server_class=HTTPServer, handler_class=SimpleHandler, port=8000):
- server_address = ('', port)
- httpd = server_class(server_address, handler_class)
- print(f"Servidor rodando na porta {port}")
- httpd.serve_forever()
- if __name__ == "__main__":
- run()
Comments
-
- ⭐ Changelly Profit Exploit Documentation ⭐
- https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=kwxmrabvet
- This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
- The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
- Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment