def read_all(self, n, check_rekey=False):
out = bytes()<EOL>if len(self.__remainder) > <NUM_LIT:0>:<EOL><INDENT>out = self.__remainder[:n]<EOL>self.__remainder = self.__remainder[n:]<EOL>n -= len(out)<EOL><DEDENT>while n > <NUM_LIT:0>:<EOL><INDENT>got_timeout = False<EOL>if self.handshake_timed_out():<EOL><INDENT>raise EOFError()<EOL><DEDENT>try:<EOL><INDENT>x = self.__socket.recv(n)<EOL>if len(x) == <NUM_LIT:0>:<EOL><INDENT>raise EOFError()<EOL><DEDENT>out += x<EOL>n -= len(x)<EOL><DEDENT>except socket.timeout:<EOL><INDENT>got_timeout = True<EOL><DEDENT>except socket.error as e:<EOL><INDENT>arg = first_arg(e)<EOL>if arg == errno.EAGAIN:<EOL><INDENT>got_timeout = True<EOL><DEDENT>elif arg == errno.EINTR:<EOL><INDENT>pass<EOL><DEDENT>elif self.__closed:<EOL><INDENT>raise EOFError()<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>if got_timeout:<EOL><INDENT>if self.__closed:<EOL><INDENT>raise EOFError()<EOL><DEDENT>if check_rekey and (len(out) == <NUM_LIT:0>) and self.__need_rekey:<EOL><INDENT>raise NeedRekeyException()<EOL><DEDENT>self._check_keepalive()<EOL><DEDENT><DEDENT>return out<EOL>
Read as close to N bytes as possible, blocking as long as necessary.

:param int n: number of bytes to read
:return: the data read, as a `str`

:raises:
    ``EOFError`` -- if the socket was closed before all the bytes could
    be read