如何在python中给定起始网络地址的情况下获取下一个网络地址 [英] How to get the next network address given the starting network address in python

查看:64
本文介绍了如何在python中给定起始网络地址的情况下获取下一个网络地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中有没有办法从给定的起始网络地址获取下一个网络地址.有很多方法可以使用 ipaddress 模块获取下一个主机 IP,但是有没有办法获取下一个网络本身?

Is there a way in python to get the next network address from the given start network address. There are ways go get the next host IP using the ipaddress module, but is there a way to get the next network itself?

例如,如果我的起始网络地址 10.1.0.0/16,那么在每次迭代时,我希望获得下一个网络地址为 10.2.0.0/1610.3.0.0/16 等.

For example, if my start network address 10.1.0.0/16, then on each iteration I would like to get the next network address as 10.2.0.0/16, 10.3.0.0/16 and so on.

因此,在 10.1.0.0/16 的情况下,网络部分是前两个八位字节 (10.1),其余部分是主机部分.我希望在每次迭代时单独增加网络部分,保持主机部分不变,例如:

So in case of 10.1.0.0/16 the network part is the first two octets(10.1) and remaining are the host part. I want the network part alone to be incremented on each iteration, leaving host part as it is, for eg:

start_network = ipaddress.ip_network('10.1.0.0/16')

# Now on each iteration I want the below output

In [37]: next(start_network)
Out[37]: IPv4Network('10.2.0.0/16')
In [38]: next(start_network)
Out[38]: IPv4Network('10.3.0.0/16')

尝试使用子网选项,但顾名思义,它将为您提供当前网络中的子网络

Tried using subnets options, but as name suggests it will get you the sub networks within the current network

In [34]: network = ipaddress.ip_network('10.1.0.0/16')

In [35]: network.subnets()

但我们的要求是获取下一个网络地址(最好不要任何字符串操作)

But our requirement is to get the next network address (preferably without any string manipulations)

推荐答案

图书馆 ipcalc有例程使 IP 地址的数学运算相当容易.例如,获取下一个网络范围地址的迭代器可以这样做:

The library ipcalc has routines to make math on ip addresses fairly easy. As an example an iterator to get the next network range address can be done like:

代码:

import ipcalc
import itertools as it

network = ipcalc.Network('10.1.0.0/16')
network_addrs = (network + (i + 1) * network.size() for i in it.count())

测试代码:

print(next(network_addrs))
print(next(network_addrs))
print(next(network_addrs))

结果:

10.2.0.0/16
10.3.0.0/16
10.4.0.0/16

仅限标准库:

如果最好不要安装ipcalc,可以构造一个继承自ipaddress.IPv4Network的类.

If it would be preferable to not install ipcalc, a class that inherits from ipaddress.IPv4Network can be constructed.

import ipaddress

class BetterIPv4Network(ipaddress.IPv4Network):

    def __add__(self, offset):
        """Add numeric offset to the IP."""
        new_base_addr = int(self.network_address) + offset
        return self.__class__((new_base_addr, str(self.netmask)))

    def size(self):
        """Return network size."""
        start = int(self.network_address)
        return int(self.broadcast_address) + 1 - start

用法:

import itertools as it

network = BetterIPv4Network(u'10.1.0.0/16')
network_addrs = (network + (i + 1) * network.size() for i in it.count())

Python 3.4:

Python 3.4 不接受元组来初始化 ipaddress.IPv4Network.这段代码可以解决这个问题.

Python 3.4 did not accept tuples to init ipaddress.IPv4Network. This code will work around that.

class BetterIPv4Network(ipaddress.IPv4Network):

    def __add__(self, offset):
        """Add numeric offset to the IP."""
        new_base_addr = int(self.network_address) + offset
        new_base_addr_str = str(self.__class__(new_base_addr)).split('/')[0]
        return self.__class__(new_base_addr_str + '/' + str(self.netmask))

    def size(self):
        """Return network size."""
        start = int(self.network_address)
        return int(self.broadcast_address) + 1 - start

这篇关于如何在python中给定起始网络地址的情况下获取下一个网络地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆