Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from http.server import SimpleHTTPRequestHandler, HTTPServer
- import os
- UPLOAD_PATH = "latest.jpg"
- class MyHandler(SimpleHTTPRequestHandler):
- def do_GET(self):
- if self.path == '/' or self.path == '/index.html':
- self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- html = """
- <html>
- <head><title>Ultima Foto</title></head>
- <body>
- <h1>Ultima imagem enviada</h1>
- <img src="/latest.jpg" width="640" id="cam">
- <script>
- setInterval(() => {
- document.getElementById("cam").src = "/latest.jpg?t=" + new Date().getTime();
- }, 2000);
- </script>
- </body>
- </html>
- """
- self.wfile.write(html.encode('utf-8'))
- elif self.path.startswith("/latest.jpg"):
- if os.path.exists(UPLOAD_PATH):
- self.send_response(200)
- self.send_header('Content-type', 'image/jpeg')
- self.send_header('Cache-Control', 'no-store')
- self.end_headers()
- with open(UPLOAD_PATH, 'rb') as f:
- self.wfile.write(f.read())
- else:
- self.send_error(404, "Nenhuma imagem disponível ainda")
- else:
- self.send_error(404, "Not found")
- def do_POST(self):
- if self.path == '/upload':
- content_length = int(self.headers['Content-Length'])
- post_body = self.rfile.read(content_length)
- with open(UPLOAD_PATH, 'wb') as f:
- f.write(post_body)
- self.send_response(200)
- self.end_headers()
- self.wfile.write(b"Imagem recebida com sucesso")
- else:
- self.send_error(404, "POST para caminho desconhecido")
- PORT = 8000
- httpd = HTTPServer(('0.0.0.0', PORT), MyHandler)
- print(f"Servidor rodando em http://0.0.0.0:{PORT}")
- httpd.serve_forever()
Add Comment
Please, Sign In to add comment