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


Python IPNetwork.subnet方法代码示例

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


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

示例1: generate_networks_for_template

# 需要导入模块: from ipaddr import IPNetwork [as 别名]
# 或者: from ipaddr.IPNetwork import subnet [as 别名]
    def generate_networks_for_template(self, template, ip_nets,
                                       ip_prefixlen):
        """Slice network to subnets for template.

        Generate networks from network template and ip_nets descriptions
        for node groups and value to slice that descriptions. ip_nets is a
        dict with key named as nodegroup and strings values for with
        description of network for that nodegroup in format '127.0.0.1/24'
        to be sliced in pieces for networks. ip_prefixlen - the amount the
        network prefix length should be sliced by. 24 will create networks
        '127.0.0.1/24' from network '127.0.0.1/16'.

        :param template: Yaml template with network assignments on interfaces.
        :param ip_nets: Dict with network descriptions.
        :param ip_prefixlen: Integer for slicing network prefix.
        :return: Data to be used to assign networks to nodes.
        """
        networks_data = []
        nodegroups = self.fuel_web.client.get_nodegroups()
        for nodegroup, section in template['adv_net_template'].items():
            networks = [(n, section['network_assignments'][n]['ep'])
                        for n in section['network_assignments']]
            assert_true(any(n['name'] == nodegroup for n in nodegroups),
                        'Network templates contains settings for Node Group '
                        '"{0}", which does not exist!'.format(nodegroup))
            group_id = [n['id'] for n in nodegroups if
                        n['name'] == nodegroup][0]
            ip_network = IPNetwork(ip_nets[nodegroup])
            ip_subnets = ip_network.subnet(
                int(ip_prefixlen) - int(ip_network.prefixlen))
            for network, interface in networks:
                ip_subnet = ip_subnets.pop()
                networks_data.append(
                    {
                        'name': network,
                        'cidr': str(ip_subnet),
                        'group_id': group_id,
                        'interface': interface,
                        'gateway': None,
                        'meta': {
                            "notation": "ip_ranges",
                            "render_type": None,
                            "map_priority": 0,
                            "configurable": True,
                            "unmovable": False,
                            "use_gateway": False,
                            "render_addr_mask": None,
                            'ip_range': [str(ip_subnet[1]), str(ip_subnet[-2])]
                        }
                    }
                )
        return networks_data
开发者ID:izadorozhna,项目名称:fuel-qa,代码行数:54,代码来源:test_net_templates_base.py

示例2: calcSubnet

# 需要导入模块: from ipaddr import IPNetwork [as 别名]
# 或者: from ipaddr.IPNetwork import subnet [as 别名]
def calcSubnet(cidr, positional=1):
  net = IPNetwork(args.CIDR)
  try:
    subnet = list(net.subnet(positional))
  except ValueError:
    finalcidr = int(cidr.split('/')[1]) + positional
    print "[%s] is out of range with [%s] positions away (A /%s, seriously?). Cannot calculate." % (cidr, positional, finalcidr)
    raise SystemExit

  newcidr = subnet[0].prefixlen
  count = len(subnet)

  print "[%s] hosts with [/%s] (diff of %s)" % (count, newcidr, positional)
  for i in subnet:
    print "--%s" % i
开发者ID:asciifaceman,项目名称:pycidrnet,代码行数:17,代码来源:pycidrnet.py

示例3: generate_networks_for_template

# 需要导入模块: from ipaddr import IPNetwork [as 别名]
# 或者: from ipaddr.IPNetwork import subnet [as 别名]
 def generate_networks_for_template(self, template, ip_network,
                                    ip_prefixlen):
     networks_data = []
     nodegroups = self.fuel_web.client.get_nodegroups()
     for nodegroup, section in template['adv_net_template'].items():
         networks = [(n, section['network_assignments'][n]['ep'])
                     for n in section['network_assignments']]
         assert_true(any(n['name'] == nodegroup for n in nodegroups),
                     'Network templates contains settings for Node Group '
                     '"{0}", which does not exist!'.format(nodegroup))
         group_id = [n['id'] for n in nodegroups if
                     n['name'] == nodegroup][0]
         ip_network = IPNetwork(ip_network)
         ip_subnets = ip_network.subnet(
             int(ip_prefixlen) - int(ip_network.prefixlen))
         for network, interface in networks:
             ip_subnet = ip_subnets.pop()
             networks_data.append(
                 {
                     'name': network,
                     'cidr': str(ip_subnet),
                     'group_id': group_id,
                     'interface': interface,
                     'gateway': None,
                     'meta': {
                         "notation": "ip_ranges",
                         "render_type": None,
                         "map_priority": 0,
                         "configurable": True,
                         "unmovable": False,
                         "use_gateway": False,
                         "render_addr_mask": None,
                         'ip_range': [str(ip_subnet[1]), str(ip_subnet[-2])]
                     }
                 }
             )
     return networks_data
开发者ID:simudream,项目名称:fuel-qa,代码行数:39,代码来源:test_net_templates_base.py


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