Python zmq.EADDRINUSE Examples

The following are 5 code examples of zmq.EADDRINUSE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zmq , or try the search function .
Example #1
Source File: socket.py    From Computable with MIT License 5 votes vote down vote up
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
        """bind this socket to a random port in a range

        Parameters
        ----------
        addr : str
            The address string without the port to pass to ``Socket.bind()``.
        min_port : int, optional
            The minimum port in the range of ports to try (inclusive).
        max_port : int, optional
            The maximum port in the range of ports to try (exclusive).
        max_tries : int, optional
            The maximum number of bind attempts to make.

        Returns
        -------
        port : int
            The port the socket was bound to.
    
        Raises
        ------
        ZMQBindError
            if `max_tries` reached before successful bind
        """
        for i in range(max_tries):
            try:
                port = random.randrange(min_port, max_port)
                self.bind('%s:%s' % (addr, port))
            except ZMQError as exception:
                if not exception.errno == zmq.EADDRINUSE:
                    raise
            else:
                return port
        raise ZMQBindError("Could not bind socket to random port.") 
Example #2
Source File: pyscard_rpc_ctrl.py    From simLAB with GNU General Public License v2.0 5 votes vote down vote up
def runServer(self, endpoint):
        self.s = zerorpc.Server(self)
        #self.s.bind(endpoint)
        try:
            self.s.bind(endpoint)
        except zmq.ZMQError as e:
            if e.errno == zmq.EADDRINUSE:
                logging.info("Pyscard RPC server already running\n")
            else:
                logging.warning("Pyscard RPC server " + endpoint + " could not be started")
            self.s.close()
            sys.exit(0)

        #create result file only when server is started
        dir = os.path.dirname(__file__)
        resultFile = dir + "/../pyscard_rpc.log"
        FORMATTER = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%H:%M:%S')
        fileHndl = logging.FileHandler(resultFile, mode='w')
        fileHndl.setFormatter(FORMATTER)
        fileHndl.setLevel(logging.DEBUG)

        logger = logging.getLogger()
        logger.addHandler(fileHndl)

        logging.info("Pyscard RPC Server " + endpoint + " started\n")

        self.s.run()
        return self 
Example #3
Source File: socket.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
        """Bind this socket to a random port in a range.

        If the port range is unspecified, the system will choose the port.

        Parameters
        ----------
        addr : str
            The address string without the port to pass to ``Socket.bind()``.
        min_port : int, optional
            The minimum port in the range of ports to try (inclusive).
        max_port : int, optional
            The maximum port in the range of ports to try (exclusive).
        max_tries : int, optional
            The maximum number of bind attempts to make.

        Returns
        -------
        port : int
            The port the socket was bound to.
    
        Raises
        ------
        ZMQBindError
            if `max_tries` reached before successful bind
        """
        if hasattr(constants, 'LAST_ENDPOINT') and min_port == 49152 and max_port == 65536:
            # if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
            # we can bind to port 0 and let the OS do the work
            self.bind("%s:*" % addr)
            url = self.last_endpoint.decode('ascii', 'replace')
            _, port_s = url.rsplit(':', 1)
            return int(port_s)
        
        for i in range(max_tries):
            try:
                port = random.randrange(min_port, max_port)
                self.bind('%s:%s' % (addr, port))
            except ZMQError as exception:
                en = exception.errno
                if en == zmq.EADDRINUSE:
                    continue
                elif sys.platform == 'win32' and en == errno.EACCES:
                    continue
                else:
                    raise
            else:
                return port
        raise ZMQBindError("Could not bind socket to random port.") 
Example #4
Source File: socket.py    From pySINDy with MIT License 4 votes vote down vote up
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
        """Bind this socket to a random port in a range.

        If the port range is unspecified, the system will choose the port.

        Parameters
        ----------
        addr : str
            The address string without the port to pass to ``Socket.bind()``.
        min_port : int, optional
            The minimum port in the range of ports to try (inclusive).
        max_port : int, optional
            The maximum port in the range of ports to try (exclusive).
        max_tries : int, optional
            The maximum number of bind attempts to make.

        Returns
        -------
        port : int
            The port the socket was bound to.
    
        Raises
        ------
        ZMQBindError
            if `max_tries` reached before successful bind
        """
        if hasattr(constants, 'LAST_ENDPOINT') and min_port == 49152 and max_port == 65536:
            # if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
            # we can bind to port 0 and let the OS do the work
            self.bind("%s:*" % addr)
            url = self.last_endpoint.decode('ascii', 'replace')
            _, port_s = url.rsplit(':', 1)
            return int(port_s)
        
        for i in range(max_tries):
            try:
                port = random.randrange(min_port, max_port)
                self.bind('%s:%s' % (addr, port))
            except ZMQError as exception:
                en = exception.errno
                if en == zmq.EADDRINUSE:
                    continue
                elif sys.platform == 'win32' and en == errno.EACCES:
                    continue
                else:
                    raise
            else:
                return port
        raise ZMQBindError("Could not bind socket to random port.") 
Example #5
Source File: socket.py    From Carnets with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
        """Bind this socket to a random port in a range.

        If the port range is unspecified, the system will choose the port.

        Parameters
        ----------
        addr : str
            The address string without the port to pass to ``Socket.bind()``.
        min_port : int, optional
            The minimum port in the range of ports to try (inclusive).
        max_port : int, optional
            The maximum port in the range of ports to try (exclusive).
        max_tries : int, optional
            The maximum number of bind attempts to make.

        Returns
        -------
        port : int
            The port the socket was bound to.
    
        Raises
        ------
        ZMQBindError
            if `max_tries` reached before successful bind
        """
        if hasattr(constants, 'LAST_ENDPOINT') and min_port == 49152 and max_port == 65536:
            # if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
            # we can bind to port 0 and let the OS do the work
            self.bind("%s:*" % addr)
            url = self.last_endpoint.decode('ascii', 'replace')
            _, port_s = url.rsplit(':', 1)
            return int(port_s)
        
        for i in range(max_tries):
            try:
                port = random.randrange(min_port, max_port)
                self.bind('%s:%s' % (addr, port))
            except ZMQError as exception:
                en = exception.errno
                if en == zmq.EADDRINUSE:
                    continue
                elif sys.platform == 'win32' and en == errno.EACCES:
                    continue
                else:
                    raise
            else:
                return port
        raise ZMQBindError("Could not bind socket to random port.")