Example #1
0
    def test_is_ip(self):
        invalid_ips = ['', '.', '\0', '-1', 'asd', '1.1.1', '2.2.2.', '1.1.1.1/', '1.1.1.1/33',
                       ':::', '1::2::2', '1:2:3:4:5:6:7:8:9', '::/129', '2::1/', '2::1/-1',
                       '1212', '...', '.....', '1.1.1.-1', '23::fg', '32344::3', '43434/22',
                       '/', '//', '/30', '/129', '2/2/2', ':::/64']
        for ip in invalid_ips:
            self.assertFalse(is_ip(ip))
            self.assertFalse(is_ipv4(ip))
            self.assertFalse(is_ipv6(ip))

        valid_cidrs = valid_v4_cidrs + valid_v6_cidrs

        for ip in valid_ips:
            self.assertTrue(is_ip(ip))
            self.assertTrue(is_cidr(ip))
            self.assertFalse(is_cidr(ip, accept_ip=False))

        for ip in valid_cidrs:
            self.assertTrue(is_cidr(ip))
            self.assertFalse(is_ip(ip))
        for ip in valid_v4_cidrs:
            self.assertTrue(is_cidr(ip, version=4))
            self.assertFalse(is_cidr(ip, version=6))
        for ip in valid_v6_cidrs:
            self.assertTrue(is_cidr(ip, version=6))
            self.assertFalse(is_cidr(ip, version=4))
Example #2
0
    def test_is_ip(self):
        invalid_ips = [
            '', '.', '\0', '-1', 'asd', '1.1.1', '2.2.2.', '1.1.1.1/',
            '1.1.1.1/33', ':::', '1::2::2', '1:2:3:4:5:6:7:8:9', '::/129',
            '2::1/', '2::1/-1', '1212', '...', '.....', '1.1.1.-1', '23::fg',
            '32344::3', '43434/22', '/', '//', '/30', '/129', '2/2/2', ':::/64'
        ]
        for ip in invalid_ips:
            self.assertFalse(is_ip(ip))
            self.assertFalse(is_ipv4(ip))
            self.assertFalse(is_ipv6(ip))

        valid_cidrs = valid_v4_cidrs + valid_v6_cidrs

        for ip in valid_ips:
            self.assertTrue(is_ip(ip))
            self.assertTrue(is_cidr(ip))
            self.assertFalse(is_cidr(ip, accept_ip=False))

        for ip in valid_cidrs:
            self.assertTrue(is_cidr(ip))
            self.assertFalse(is_ip(ip))
        for ip in valid_v4_cidrs:
            self.assertTrue(is_cidr(ip, version=4))
            self.assertFalse(is_cidr(ip, version=6))
        for ip in valid_v6_cidrs:
            self.assertTrue(is_cidr(ip, version=6))
            self.assertFalse(is_cidr(ip, version=4))
Example #3
0
def make_socket_for_ip(ip, stype=socket.SOCK_STREAM):
    """Create a socket object with the correct address family for ip.

    :Parameters:
        - `ip`: An IP address as a string
        - `stype`: The socket type (see `SOCK`).

    :Return:
        Returns a socket object.

    :Exceptions:
        - `OSError`: OS-level error.
        - `ValueError`: Invalid IP address.
    """

    if net_ip.is_ipv4(ip):
        return coro.make_socket(socket.AF_INET, stype)
    elif net_ip.is_ipv6(ip):
        return coro.make_socket(socket.AF_INET6, stype)
    else:
        raise ValueError("Invalid IP address")
Example #4
0
    def get_new_connection(self, bind_address):
        """Return a new connection"""
        if len(self.connections) >= self.max_connections:
            raise MaxConnectionsLimit(self.host, self.port)
        else:
            try:
                self.no_of_connections += 1
                if ip.is_ipv6(self.host):
                    bind_ip = bind_address[1]
                else:
                    bind_ip = bind_address[0]

                if bind_ip is None:
                    raise IfaceNotCompatible(self.host, self.port)
                new_connection = self.conn_class(self.host, self.port, bind_ip)
            except:
                self.no_of_connections -= 1
                raise

            new_connection.mark_used()
            self.connections.append(new_connection)

        return new_connection
Example #5
0
    def get_new_connection(self, bind_address):
        """Return a new connection"""
        if len(self.connections) >= self.max_connections:
            raise MaxConnectionsLimit(self.host, self.port)
        else:
            try:
                self.no_of_connections += 1
                if ip.is_ipv6(self.host):
                    bind_ip = bind_address[1]
                else:
                    bind_ip = bind_address[0]

                if bind_ip is None:
                    raise IfaceNotCompatible(self.host, self.port)
                new_connection = self.conn_class(self.host, self.port,
                                                 bind_ip)
            except:
                self.no_of_connections -= 1
                raise

            new_connection.mark_used()
            self.connections.append(new_connection)

        return new_connection