that_one_nigga_you_k

codigo do servidor que recebe fotos e bota no site

May 21st, 2025 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. from http.server import SimpleHTTPRequestHandler, HTTPServer
  2. import os
  3.  
  4. UPLOAD_PATH = "latest.jpg"
  5.  
  6. class MyHandler(SimpleHTTPRequestHandler):
  7.     def do_GET(self):
  8.         if self.path == '/' or self.path == '/index.html':
  9.             self.send_response(200)
  10.             self.send_header('Content-type', 'text/html')
  11.             self.end_headers()
  12.             html = """
  13.                <html>
  14.                <head><title>Ultima Foto</title></head>
  15.                <body>
  16.                    <h1>Ultima imagem enviada</h1>
  17.                    <img src="/latest.jpg" width="640" id="cam">
  18.                    <script>
  19.                        setInterval(() => {
  20.                            document.getElementById("cam").src = "/latest.jpg?t=" + new Date().getTime();
  21.                        }, 2000);
  22.                    </script>
  23.                </body>
  24.                </html>
  25.            """
  26.             self.wfile.write(html.encode('utf-8'))
  27.  
  28.         elif self.path.startswith("/latest.jpg"):
  29.             if os.path.exists(UPLOAD_PATH):
  30.                 self.send_response(200)
  31.                 self.send_header('Content-type', 'image/jpeg')
  32.                 self.send_header('Cache-Control', 'no-store')
  33.                 self.end_headers()
  34.                 with open(UPLOAD_PATH, 'rb') as f:
  35.                     self.wfile.write(f.read())
  36.             else:
  37.                 self.send_error(404, "Nenhuma imagem disponível ainda")
  38.         else:
  39.             self.send_error(404, "Not found")
  40.  
  41.     def do_POST(self):
  42.         if self.path == '/upload':
  43.             content_length = int(self.headers['Content-Length'])
  44.             post_body = self.rfile.read(content_length)
  45.  
  46.             with open(UPLOAD_PATH, 'wb') as f:
  47.                 f.write(post_body)
  48.  
  49.             self.send_response(200)
  50.             self.end_headers()
  51.             self.wfile.write(b"Imagem recebida com sucesso")
  52.         else:
  53.             self.send_error(404, "POST para caminho desconhecido")
  54.  
  55. PORT = 8000
  56. httpd = HTTPServer(('0.0.0.0', PORT), MyHandler)
  57. print(f"Servidor rodando em http://0.0.0.0:{PORT}")
  58. httpd.serve_forever()
  59.  
Add Comment
Please, Sign In to add comment