当前位置: 首页>>代码示例>>Python>>正文


Python Network.network方法代码示例

本文整理汇总了Python中ipcalc.Network.network方法的典型用法代码示例。如果您正苦于以下问题:Python Network.network方法的具体用法?Python Network.network怎么用?Python Network.network使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ipcalc.Network的用法示例。


在下文中一共展示了Network.network方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_context_data

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
    def get_context_data(self, **kwargs):
        from django.db.models import Sum
        from ipam.models import BLOCK_STATUSES

        def search_free(vrf, network, statuses):
            """
            Free blocks recursive search
            :param vrf: VRF
            :type vrf: Vrf
            :param network:  Network
            :type network: Network
            """
            f_ip = network.ip
            l_ip = network.broadcast_long()
            size = network.size()
            blocked_size = \
                vrf.prefixes().filter(status__in=statuses, first_ip_dec__gte=f_ip, last_ip_dec__lte=l_ip).aggregate(
                    sum_size=Sum('size'))['sum_size']
            if not blocked_size:
                blocked_size = 0
            if size == blocked_size:
                return []
            elif blocked_size == 0:
                if vrf.prefixes().filter(first_ip_dec__lte=f_ip, last_ip_dec__gte=l_ip):
                    parent = vrf.prefixes().filter(first_ip_dec__lte=f_ip, last_ip_dec__gte=l_ip).last()
                else:
                    parent = None
                if size == 1:
                    create_url = '{0}?prefix={1}'.format(reverse_lazy('ipam.host4_add', kwargs={'vrf': vrf.name}),
                                                         network.dq)
                else:
                    create_url = '{0}?prefix={1}'.format(reverse_lazy('ipam.prefix4_add', kwargs={'vrf': vrf.name}),
                                                         network)
                return [{'prefix': str(network), 'parent': parent, 'create_url': create_url}]
            else:
                net_1 = Network('{0}/{1}'.format(network.to_tuple()[0], network.to_tuple()[1] + 1))
                net_2 = Network('{0}/{1}'.format(IP(net_1.broadcast_long() + 1).dq, network.subnet() + 1))
                return search_free(vrf, net_1, statuses) + search_free(vrf, net_2, statuses)

        context = super(FreeBlocks4ReportView, self).get_context_data(**kwargs)
        context['page_title'] = u'Free IPv4 Blocks'
        if 'vrf' in self.request.GET and 'prefix' in self.request.GET:
            if Vrf.objects.filter(name=self.request.GET['vrf']):
                net = Network(self.request.GET['prefix'])
                if net.dq != net.network():
                    net = Network('{0}/{1}'.format(net.network(), net.subnet()))
                context['report'] = search_free(Vrf.objects.get(name=self.request.GET['vrf']), net, BLOCK_STATUSES)
                context['total'] = len(context['report'])
        return context
开发者ID:k-vinogradov,项目名称:noclite,代码行数:51,代码来源:views.py

示例2: clean

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
    def clean(self):
        super(Prefix4, self).clean()
        prefix = self.prefix
        if re.match(r'(\d{1,3}\.){3}\d{1,3}[\s]*$', prefix):
            self.prefix = prefix + '/32'
        elif re.match(r'(\d{1,3}\.){3}\d{1,3}/\d{1,2}', prefix):
            pass
        else:
            raise ValidationError(u'Invalid network prefix "{0}"'.format(prefix))
        network = Network(str(self.prefix))

        if network.network().dq != network.dq:
            raise ValidationError(u'Invalid prefix length /{0}'
                                  u' for the network {1}'.format(self.prefix.split('/')[1], self.prefix.split('/')[0]))

        # qs = self.vrf_list(exclude_self=True)

        if not self.find_parent() and not self.domain:
            ValidationError(u'Top-level prefix must have domain name')

        if self.status in BLOCK_STATUSES:
            p = self.prefixes_lower(statuses=BLOCK_STATUSES).first()
            if not p:
                p = self.prefixes_upper(statuses=BLOCK_STATUSES).last()
            if p:
                raise ValidationError(u'Network {0} is already {1}'.format(p.prefix, p.get_status_display().lower()))
开发者ID:k-vinogradov,项目名称:noclite,代码行数:28,代码来源:models.py

示例3: add_network_cidr_mapping

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
    def add_network_cidr_mapping(self,
        network_config):
        '''
        Method calculates and adds a CloudFormation mapping that is used to set VPC and Subnet CIDR blocks.  Calculated based on CIDR block sizes and additionally checks to ensure all network segments fit inside of the specified overall VPC CIDR
        @param network_config [dict] dictionary of values containing data for creating
        '''
        public_subnet_count = int(network_config.get('public_subnet_count', 2))
        private_subnet_count = int(network_config.get('private_subnet_count', 2))
        public_subnet_size = str(network_config.get('public_subnet_size', '24'))
        private_subnet_size = str(network_config.get('private_subnet_size', '22'))
        network_cidr_base = str(network_config.get('network_cidr_base', '172.16.0.0'))
        network_cidr_size = str(network_config.get('network_cidr_size', '20'))
        first_network_address_block = str(network_config.get('first_network_address_block', network_cidr_base))

        ret_val = {}
        cidr_info = Network(network_cidr_base + '/' + network_cidr_size)
        base_cidr = cidr_info.network().to_tuple()[0] + '/' + str(cidr_info.to_tuple()[1])
        ret_val['vpcBase'] = {'cidr': base_cidr}
        current_base_address = first_network_address_block
        for public_subnet_id in range(0, public_subnet_count):
            if not cidr_info.check_collision(current_base_address):
                raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(public_subnet_id + 1) + ' when creating public subnet cidrs')
            ip_info = Network(current_base_address + '/' + str(public_subnet_size))
            range_info = ip_info.network().to_tuple()
            if 'subnet' + str(public_subnet_id) not in ret_val:
                ret_val['subnet' + str(public_subnet_id)] = dict()
            ret_val['subnet' + str(public_subnet_id)]['public'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
            current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
        range_reset = Network(current_base_address + '/' + str(private_subnet_size))
        current_base_address = IP(int(range_reset.host_last().hex(), 16) + 2).to_tuple()[0]
        for private_subnet_id in range(0, private_subnet_count):
            if not cidr_info.check_collision(current_base_address):
                raise RuntimeError('Cannot continue creating network--current base address is outside the range of the master Cidr block. Found on pass ' + str(private_subnet_id + 1) + ' when creating private subnet cidrs')
            ip_info = Network(current_base_address + '/' + str(private_subnet_size))
            range_info = ip_info.network().to_tuple()
            if 'subnet' + str(private_subnet_id) not in ret_val:
                ret_val['subnet' + str(private_subnet_id)] = dict()
            ret_val['subnet' + str(private_subnet_id)]['private'] = ip_info.network().to_tuple()[0] + '/' + str(ip_info.to_tuple()[1])
            current_base_address = IP(int(ip_info.host_last().hex(), 16) + 2).to_tuple()[0]
        return self.template.add_mapping('networkAddresses', ret_val)
开发者ID:JonGal,项目名称:cloudformation-environmentbase,代码行数:42,代码来源:networkbase.py

示例4: new_isp_ip

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
def new_isp_ip():
    with open('new_isp_ip.txt', 'w') as f:
        for line in open('isp_ip.txt'):
            line = line.strip()
            if line:
                elems = [e.strip() for e in line.split('/')]
                try:
                    mask = int(elems[1])
                except ValueError:
                    f.write(','.join(elems) + '\n')
                    continue
                network = Network(elems[0], mask=mask)
                elems[0] = network.network().dq
                elems[1] = network.broadcast().dq
                f.write(','.join(elems) + '\n')
开发者ID:llh911001,项目名称:geo-IP-country,代码行数:17,代码来源:utils.py

示例5: test_ipv4_3

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv4_3(self):
     net = Network('10.10.0.0/255.255.255.0')
     self.assertTrue(str(net) == '10.10.0.0/24')
     self.assertTrue(str(net.to_ipv6().to_compressed()) == '2002:a0a::')
     self.assertTrue(net.info() == 'PRIVATE')
     self.assertTrue(net.subnet() == 24)
     self.assertTrue(net.size() == 1 << (32 - 24))
     self.assertTrue(int(net) == 0x0a0a0000)
     self.assertTrue(net.hex().lower() == '0a0a0000')
     self.assertTrue(str(net.netmask()) == '255.255.255.0')
     self.assertTrue(net.version() == 4)
     self.assertTrue(str(net.network()) == '10.10.0.0')
     self.assertTrue(str(net.broadcast()) == '10.10.0.255')
     self.assertFalse('192.168.0.1' in net)
     self.assertFalse('192.168.114.128' in net)
     self.assertFalse('10.0.0.1' in net)
     self.assertTrue('10.10.0.254' in net)
     self.assertTrue('10.10.0.100' in net)
开发者ID:panaceya,项目名称:ipcalc,代码行数:20,代码来源:test.py

示例6: test_ipv4_3

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv4_3(self):
     net = Network("10.10.0.0/255.255.255.0")
     self.assertTrue(str(net) == "10.10.0.0/24")
     self.assertTrue(str(net.to_ipv6().to_compressed()) == "2002:a0a::")
     self.assertTrue(net.info() == "PRIVATE")
     self.assertTrue(net.subnet() == 24)
     self.assertTrue(net.size() == 1 << (32 - 24))
     self.assertTrue(int(net) == 0x0A0A0000)
     self.assertTrue(net.hex().lower() == "0a0a0000")
     self.assertTrue(str(net.netmask()) == "255.255.255.0")
     self.assertTrue(net.version() == 4)
     self.assertTrue(str(net.network()) == "10.10.0.0")
     self.assertTrue(str(net.broadcast()) == "10.10.0.255")
     self.assertFalse("192.168.0.1" in net)
     self.assertFalse("192.168.114.128" in net)
     self.assertFalse("10.0.0.1" in net)
     self.assertTrue("10.10.0.254" in net)
     self.assertTrue("10.10.0.100" in net)
开发者ID:osilva,项目名称:ipcalc,代码行数:20,代码来源:test.py

示例7: test_ipv4_1

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv4_1(self):
     net = Network('192.168.114.42', 23)
     self.assertTrue(str(net) == '192.168.114.42/23')
     self.assertTrue(str(net.to_ipv6().to_compressed()) == '2002:c0a8:722a::')
     self.assertTrue(net.info() == 'PRIVATE')
     self.assertTrue(net.subnet() == 23)
     self.assertTrue(net.size() == 1 << (32 - 23))
     self.assertTrue(int(net) == 0xc0a8722a)
     self.assertTrue(net.hex().lower() == 'c0a8722a')
     self.assertTrue(str(net.netmask()) == '255.255.254.0')
     self.assertTrue(net.version() == 4)
     self.assertTrue(str(net.network()) == '192.168.114.0')
     self.assertTrue(str(net.broadcast()) == '192.168.115.255')
     self.assertFalse('192.168.0.1' in net)
     self.assertTrue('192.168.114.128' in net)
     self.assertFalse('10.0.0.1' in net)
     self.assertTrue(str(net + 6) == '192.168.114.48/23')
     self.assertTrue((net + 6) in net)
     self.assertTrue(str(net - 6) == '192.168.114.36/23')
     self.assertTrue((net - 6) in net)
开发者ID:panaceya,项目名称:ipcalc,代码行数:22,代码来源:test.py

示例8: test_ipv6_4

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv6_4(self):
     net = Network('2001:dead:beef:1:c01d:c01a::', 'ffff:ffff:ffff::')
     self.assertTrue(str(net) == '2001:dead:beef:0001:c01d:c01a:0000:0000/48')
     self.assertTrue(str(net.to_compressed()) == '2001:dead:beef:1:c01d:c01a::')
     self.assertTrue(str(net.to_ipv6().to_compressed()) == '2001:dead:beef:1:c01d:c01a::')
     self.assertTrue(net.info() == 'UNKNOWN')
     self.assertTrue(net.subnet() == 48)
     self.assertTrue(net.size() == 1 << (128 - 48))
     self.assertTrue(int(net) == 0x2001deadbeef0001c01dc01a00000000)
     self.assertTrue(net.hex().lower() == '2001deadbeef0001c01dc01a00000000')
     self.assertTrue(str(net.netmask()) == 'ffff:ffff:ffff:0000:0000:0000:0000:0000')
     self.assertTrue(net.version() == 6)
     self.assertTrue(str(net.network()) == '2001:dead:beef:0000:0000:0000:0000:0000')
     self.assertTrue(str(net.broadcast()) == '2001:dead:beef:ffff:ffff:ffff:ffff:ffff')
     self.assertFalse('123:456::' in net)
     self.assertFalse('::aaaa:bbbb:cccc:dddd' in net)
     self.assertFalse('::dddd' in net)
     self.assertFalse('::1' in net)
     self.assertFalse('123::456' in net)
     self.assertTrue('2001:dead:beef:babe::1234' in net)
开发者ID:panaceya,项目名称:ipcalc,代码行数:22,代码来源:test.py

示例9: test_ipv4_1

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv4_1(self):
     net = Network("192.168.114.42", 23)
     self.assertTrue(str(net) == "192.168.114.42/23")
     self.assertTrue(str(net.to_ipv6().to_compressed()) == "2002:c0a8:722a::")
     self.assertTrue(net.info() == "PRIVATE")
     self.assertTrue(net.subnet() == 23)
     self.assertTrue(net.size() == 1 << (32 - 23))
     self.assertTrue(int(net) == 0xC0A8722A)
     self.assertTrue(net.hex().lower() == "c0a8722a")
     self.assertTrue(str(net.netmask()) == "255.255.254.0")
     self.assertTrue(net.version() == 4)
     self.assertTrue(str(net.network()) == "192.168.114.0")
     self.assertTrue(str(net.broadcast()) == "192.168.115.255")
     self.assertFalse("192.168.0.1" in net)
     self.assertTrue("192.168.114.128" in net)
     self.assertFalse("10.0.0.1" in net)
     self.assertTrue(str(net + 6) == "192.168.114.48/23")
     self.assertTrue((net + 6) in net)
     self.assertTrue(str(net - 6) == "192.168.114.36/23")
     self.assertTrue((net - 6) in net)
开发者ID:osilva,项目名称:ipcalc,代码行数:22,代码来源:test.py

示例10: test_ipv6_4

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv6_4(self):
     net = Network("2001:dead:beef:1:c01d:c01a::", "ffff:ffff:ffff::")
     self.assertTrue(str(net) == "2001:dead:beef:0001:c01d:c01a:0000:0000/48")
     self.assertTrue(str(net.to_compressed()) == "2001:dead:beef:1:c01d:c01a::")
     self.assertTrue(str(net.to_ipv6().to_compressed()) == "2001:dead:beef:1:c01d:c01a::")
     self.assertTrue(net.info() == "UNKNOWN")
     self.assertTrue(net.subnet() == 48)
     self.assertTrue(net.size() == 1 << (128 - 48))
     self.assertTrue(int(net) == 0x2001DEADBEEF0001C01DC01A00000000)
     self.assertTrue(net.hex().lower() == "2001deadbeef0001c01dc01a00000000")
     self.assertTrue(str(net.netmask()) == "ffff:ffff:ffff:0000:0000:0000:0000:0000")
     self.assertTrue(net.version() == 6)
     self.assertTrue(str(net.network()) == "2001:dead:beef:0000:0000:0000:0000:0000")
     self.assertTrue(str(net.broadcast()) == "2001:dead:beef:ffff:ffff:ffff:ffff:ffff")
     self.assertFalse("123:456::" in net)
     self.assertFalse("::aaaa:bbbb:cccc:dddd" in net)
     self.assertFalse("::dddd" in net)
     self.assertFalse("::1" in net)
     self.assertFalse("123::456" in net)
     self.assertTrue("2001:dead:beef:babe::1234" in net)
开发者ID:osilva,项目名称:ipcalc,代码行数:22,代码来源:test.py

示例11: test_ipv6_1

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv6_1(self):
     net = Network('123::', 128)
     self.assertTrue(str(net) == '0123:0000:0000:0000:0000:0000:0000:0000/128')
     self.assertTrue(str(net.to_compressed()) == '123::')
     self.assertTrue(str(net.to_ipv6().to_compressed()) == '123::')
     self.assertTrue(net.info() == 'UNKNOWN')
     self.assertTrue(net.subnet() == 128)
     self.assertTrue(net.size() == 1 << (128 - 128))
     self.assertTrue(int(net) == (0x123<<112))
     self.assertTrue(net.hex().lower() == '01230000000000000000000000000000')
     self.assertTrue(str(net.netmask()) == 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
     self.assertTrue(net.version() == 6)
     self.assertTrue(str(net.network()) == '0123:0000:0000:0000:0000:0000:0000:0000')
     self.assertTrue(str(net.broadcast()) == '0123:0000:0000:0000:0000:0000:0000:0000')
     self.assertFalse('123:456::' in net)
     self.assertTrue('123::' in net)
     self.assertFalse('::1' in net)
     self.assertFalse('123::456' in net)
     self.assertTrue(str((net + 6).to_compressed()).lower() == '123::6')
     self.assertFalse((net + 6) in net)
     self.assertTrue(str((net - 6).to_compressed()).lower() == '122:ffff:ffff:ffff:ffff:ffff:ffff:fffa')
     self.assertFalse((net - 6) in net)
开发者ID:panaceya,项目名称:ipcalc,代码行数:24,代码来源:test.py

示例12: test_ipv6_1

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv6_1(self):
     net = Network("123::", 128)
     self.assertTrue(str(net) == "0123:0000:0000:0000:0000:0000:0000:0000/128")
     self.assertTrue(str(net.to_compressed()) == "123::")
     self.assertTrue(str(net.to_ipv6().to_compressed()) == "123::")
     self.assertTrue(net.info() == "UNKNOWN")
     self.assertTrue(net.subnet() == 128)
     self.assertTrue(net.size() == 1 << (128 - 128))
     self.assertTrue(int(net) == (0x123 << 112))
     self.assertTrue(net.hex().lower() == "01230000000000000000000000000000")
     self.assertTrue(str(net.netmask()) == "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
     self.assertTrue(net.version() == 6)
     self.assertTrue(str(net.network()) == "0123:0000:0000:0000:0000:0000:0000:0000")
     self.assertTrue(str(net.broadcast()) == "0123:0000:0000:0000:0000:0000:0000:0000")
     self.assertFalse("123:456::" in net)
     self.assertTrue("123::" in net)
     self.assertFalse("::1" in net)
     self.assertFalse("123::456" in net)
     self.assertTrue(str((net + 6).to_compressed()).lower() == "123::6")
     self.assertFalse((net + 6) in net)
     self.assertTrue(str((net - 6).to_compressed()).lower() == "122:ffff:ffff:ffff:ffff:ffff:ffff:fffa")
     self.assertFalse((net - 6) in net)
开发者ID:osilva,项目名称:ipcalc,代码行数:24,代码来源:test.py

示例13: test_ipv6_2

# 需要导入模块: from ipcalc import Network [as 别名]
# 或者: from ipcalc.Network import network [as 别名]
 def test_ipv6_2(self):
     net = Network('::42', 64)
     self.assertTrue(str(net) == '0000:0000:0000:0000:0000:0000:0000:0042/64')
     self.assertTrue(str(net.to_compressed()) == '::42')
     self.assertTrue(str(net.to_ipv6().to_compressed()) == '::42')
     self.assertTrue(net.info() == 'IPV4COMP')
     self.assertTrue(net.subnet() == 64)
     self.assertTrue(net.size() == 1 << (128 - 64))
     self.assertTrue(int(net) == 0x42)
     self.assertTrue(net.hex().lower() == '00000000000000000000000000000042')
     self.assertTrue(str(net.netmask()) == 'ffff:ffff:ffff:ffff:0000:0000:0000:0000')
     self.assertTrue(net.version() == 6)
     self.assertTrue(str(net.network()) == '0000:0000:0000:0000:0000:0000:0000:0000')
     self.assertTrue(str(net.broadcast()) == '0000:0000:0000:0000:ffff:ffff:ffff:ffff')
     self.assertFalse('123:456::' in net)
     self.assertTrue('::aaaa:bbbb:cccc:dddd' in net)
     self.assertTrue('::dddd' in net)
     self.assertTrue('::1' in net)
     self.assertFalse('123::456' in net)
     self.assertTrue(str((net + 6).to_compressed()).lower() == '::48')
     self.assertTrue((net + 6) in net)
     self.assertTrue(str((net - 6).to_compressed()).lower() == '::3c')
     self.assertTrue((net - 6) in net)
开发者ID:panaceya,项目名称:ipcalc,代码行数:25,代码来源:test.py


注:本文中的ipcalc.Network.network方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。