def work(input_queue=self.input_queue, output_queue=self.output_queue): while True: try: data, addr = input_queue.get() request = dns.parse(data) response = handler.handle(request) output = dns.construct_response(request, response) output_queue.put((output, addr)) except KeyboardInterrupt: break except Exception as e: logging.error('Got exception: %r', e, exc_info=True)
def handle_read(self): data = self.recv(514) if not data: return # Add to the buffer self.buffer += data data = self.buffer if len(data) < 2: return if self.size is None: self.size, = struct.unpack('!H', data[:2]) packet = data[2:2 + self.size] diff = len(packet) - self.size if diff < 0: return else: self.buffer = self.buffer[2 + len(packet):] request = dns.parse(packet) response = self.handler.handle(request) output = dns.construct_response(request, response) self.send(struct.pack('!H', len(output)) + output) self.close()