def sendCatImage(self, image): self.send_response(200) self.send_header("Content-type", "image/jpg") self.end_headers() self.wfile.write(image.tostring()) self.wfile.close() ledborg.flashColour(ledborg.GREEN)
def do_GET(self): #Ignore favicon requests to keep logs clean if "/favicon.ico" in self.path: pass #Only continue if the server is asking for a known URI. Send #403 Forbidden HTTP response otherwise. elif "/feed" in self.path: #feed cats feeder.feed() self.send_response(200, "Fed OK") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write("<!DOCTYPE html>") self.wfile.write("<html><head><title>" + os.path.basename(__file__) + "</title></head>") self.wfile.write("<body><p>Fed OK!</p>") self.wfile.write("</body></html>") self.wfile.close() ledborg.flashColour(ledborg.YELLOW) elif "/sound" in self.path: #play sound pygame.mixer.music.load("sound.ogg") pygame.mixer.music.play() self.send_response(200, "Sound played OK") self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write("<!DOCTYPE html>") self.wfile.write("<html><head><title>" + os.path.basename(__file__) + "</title></head>") self.wfile.write("<body><p>Sound played OK!</p>") self.wfile.write("</body></html>") self.wfile.close() ledborg.flashColour(ledborg.BLUE) elif "/pir.jpeg" in self.path: self.sendCatImage(motionimage) elif "/cats.jpeg" in self.path: try: status, liveImage = captureImage(True) if status: self.sendCatImage(liveImage) else: #Something went wrong while creating the image, #Send 500 Internal Server Error self.send_error(500, "Image capture failed") ledborg.flashColour(ledborg.MAGENTA) except IOError: self.send_error(404, "File Not Found: %s" % self.path) ledborg.flashColour(ledborg.MAGENTA) else: #Unknown URI self.send_error(403, "Forbidden") log("Headers in forbidden request:") for line in self.headers: log(line + ": " + self.headers.get(line)) ledborg.flashColour(ledborg.RED)