def close(self, active=True): """ This method will close the socket. The active parameter will specify whether the socket is closed on this side or was closed on the other side. If the socket was closed on this side, no attempt to reconnect will be attempted. Otherwise, a reconnect will be attempted. """ if not self.__socket or not self.__connected: return if self.__unix_socket: if self.__listener: close_what = "Unix Domain Socket listening on %s" \ % (self.__port) else: close_what = "Unix Domain Socket connection to %s" \ % (self.__port) else: if self.__listener: close_what = "server socket on port %d" % (self.__port) else: close_what = "TCP connection to %s:%d" \ % (self.__host, self.__port) self.__connected = False mgr = IOManager() mgr.unregister(self) if not self.__socket is None: try: self.__socket.close() self.__socket.shutdown() except socket.error as e: pass if active: self.__logger.info("Closed %s" % close_what) else: self.__logger.info("%s closed." % close_what) if self.__unix_socket and self.__listener: try: os.unlink(self.__port) except: pass self.__socket = None self.__fileno = None # If remote end closed connection, attempt to reconnect if not active and \ not self.__listener and \ not self.__handler and \ self.__giveup != 0: mgr = IOManager() mgr.register_unconnected(self) else: self.handle_shutdown()