Exemplo n.º 1
0
 def receive(self):
     """
     :rtype: bytes
     """
     try:
         return self.socket.recv(1024)
     except socket.error:
         _, e, _ = sys.exc_info()
         if get_errno(e) in (errno.EAGAIN, errno.EINTR):
             log.debug("socket read interrupted, restarting")
             raise exception.InterruptedException()
         if self.is_connected():
             raise
Exemplo n.º 2
0
    def disconnect_socket(self):
        """
        Disconnect the underlying socket connection
        """
        self.running = False
        if self.socket is not None:
            if self.__need_ssl():
                #
                # Even though we don't want to use the socket, unwrap is the only API method which does a proper SSL
                # shutdown
                #
                try:
                    self.socket = self.socket.unwrap()
                except Exception:
                    #
                    # unwrap seems flaky on Win with the back-ported ssl mod, so catch any exception and log it
                    #
                    _, e, _ = sys.exc_info()
                    log.warning(e)
            elif hasattr(socket, 'SHUT_RDWR'):
                try:
                    self.socket.shutdown(socket.SHUT_RDWR)
                except socket.error:
                    _, e, _ = sys.exc_info()
                    # ignore when socket already closed
                    if get_errno(e) != errno.ENOTCONN:
                        log.warning(
                            "Unable to issue SHUT_RDWR on socket because of error '%s'",
                            e)

        #
        # split this into a separate check, because sometimes the socket is nulled between shutdown and this call
        #
        if self.socket is not None:
            try:
                self.socket.close()
            except socket.error:
                _, e, _ = sys.exc_info()
                log.warning("Unable to close socket because of error '%s'", e)
        self.current_host_and_port = None
        self.socket = None
        self.notify('disconnected')