def _handshake_select_fn(self):
        """Handle client data during the handshake."""
        try:
            data = self.socket.recv(8192)
        except socket.error:
            self.logger.info("Blame: Erorr reading from client %s.", self.address)
            return False
        if not data:
            self.logger.info("Blame: Client %s closed connection.", self.address)
            return False
        data = self._buffer + data
        lines = data.split("\n")
        # Check if there is still more data to be read.
        # Some clients send \r\n line endings and some \n, so strip extra
        # whitespace.
        if lines[-1].strip() != "":
            self._buffer = data
            return
        data = data.replace("\r", "")
        lines = data[:data.index("\n\n")].split("\n")
        try:
            self._parse_headers(lines)
            self._send_headers()
        except (ValueError, KeyError, IndexError, socket.error) as e:
            try:
                self.socket.sendall("400 Error parsing message\n\n")
            except socket.error:
                pass
            self.logger.info("Blame: Bad handshake from %s: %s" % (self.address, e))
            return False
        # TODO: Handle any extra data after the handshake, there shouldn't be
        # any in the current version of the protocol.
        # Done!
        self.logger.info("Blame: New client from %s", self.address)
        self._select_fn = self._response_select_fn
        self._handshake_completed = True
        return True
Handle client data during the handshake.
nogotofail/mitm/blame/app_blame.py