如何在不同的时间间隔python递增?
How to increment at different intervals python?
说我有一个 IP 地址 192.168.1.1
我如何像这样以不同的时间间隔递增?
192.168.1.2
192.168.1.3
...一直到 192.168.1.999
然后 "roll over" 到 192.168.2.1
?
for ip3 in range(1, 127):
for ip4 in range(1, 999):
ip = '.'.join("192", "68", str(ip3), str(ip4))
这会给你灵感吗?
# for IP addresses of the form 1.92.168.a.b
a_max = 10
b_max = 999
ips = []
for a in range(a_max):
for b in range(b_max):
ips += ['1.92.168.{}.{}'.format(a,b)]
这将为您提供以字符串形式生成的所有 IP 地址的列表。
您可以使用 ip_address.split(.)
拆分地址,得到 ip_list
。跟踪您心中的限制。在转换为整数后增加 ip_list
的每个元素,然后转换回字符串并使用 ".".join(ip_list)
将它们全部放回一起
您可以使用 __add__
方法创建 class:
class Ip:
def __init__(self, _ip:str, _stop = 999) -> None:
self.ip = _ip
self.stop = _stop
def __add__(self, _val):
_ip = list(map(int, self.ip.split('.')))
_r = 0
while _r < 4:
result = _ip[3 -_r]+_val
if result < self.stop:
_ip[3 - _r] += _val
break
_ip[3 - _r] += _val%self.stop
_val = _val%self.stop
_r += 1
_ip[4 - _r] = 1
return Ip('.'.join(map(str, _ip)))
def __repr__(self):
return f'<IP {self.ip}>'
p = Ip('192.168.1.1')
new_p = p + 1
print(new_p)
print(p + 1000)
输出:
<IP 192.168.1.2>
<IP 192.168.2.1>
您应该为此使用标准库 ipaddress
。不要尝试用原始字符串操作来完成它,因为有几个陷阱和奇怪的边缘情况。使用面向对象的库将防止您生成无效数据。
迭代从 192.168.1.0 到 192.168.1.255 的 ipv4 地址,等同于迭代 192.168.1.0/24 subnet. Use a network 对象:
>>> from ipaddress import ip_network
>>> list(ip_network('192.168.1.0/24'))
[IPv4Address('192.168.1.0'),
IPv4Address('192.168.1.1'),
IPv4Address('192.168.1.2'),
...
IPv4Address('192.168.1.255')]
其中一些地址不是可用的主机,例如 255 是 broadcast address. Iterate the hosts,如果您要找的是:
>>>> list(ip_network('192.168.1.0/24').hosts())
[IPv4Address('192.168.1.1'),
IPv4Address('192.168.1.2'),
IPv4Address('192.168.1.3'),
...
IPv4Address('192.168.1.254')]
请注意,192.168.1.999 不是有效的 IP 地址,所以不要生成它!验证器无论如何都会阻止你创建它:
>>> from ipaddress import ip_address
>>> ip_address('192.168.1.254')
IPv4Address('192.168.1.254')
>>> ip_address('192.168.1.999')
# ValueError: '192.168.1.999' does not appear to be an IPv4 or IPv6 address
要将 ip address object 转换回普通的旧字符串,只需对其调用 str
。
您的问题还询问了关于 "rolling over" 到 192.168.2.1 的问题。这只是迭代不同的子网。 192.168.1.0/24中的24是指分配给网络前缀的24位有效位(其余8位保留给主机寻址),即子网掩码 255.255.255.0.
要做到 "rollover",您真的只想迭代一个更大的子网:
>>> gen = iter(ip_network('192.168.0.0/16'))
>>> for i in range(255*2):
... next(gen)
...
>>> next(gen)
IPv4Address('192.168.1.254')
>>> next(gen)
IPv4Address('192.168.1.255')
>>> next(gen)
IPv4Address('192.168.2.0')
>>> next(gen)
IPv4Address('192.168.2.1')
要从单个ip地址字符串获取网络对象,可以使用supernet
方法:
>>> from ipaddress import ip_address, ip_interface
>>> ip = ip_address('192.168.1.1')
>>> net = ip_interface(ip).network
>>> net
IPv4Network('192.168.1.1/32')
>>> net.supernet(prefixlen_diff=8)
IPv4Network('192.168.1.0/24')
阅读 Internet 协议版本 4 了解更多信息,以及 Python 的 ipaddress
模块的官方文档。
说我有一个 IP 地址 192.168.1.1
我如何像这样以不同的时间间隔递增?
192.168.1.2
192.168.1.3
...一直到 192.168.1.999
然后 "roll over" 到 192.168.2.1
?
for ip3 in range(1, 127):
for ip4 in range(1, 999):
ip = '.'.join("192", "68", str(ip3), str(ip4))
这会给你灵感吗?
# for IP addresses of the form 1.92.168.a.b
a_max = 10
b_max = 999
ips = []
for a in range(a_max):
for b in range(b_max):
ips += ['1.92.168.{}.{}'.format(a,b)]
这将为您提供以字符串形式生成的所有 IP 地址的列表。
您可以使用 ip_address.split(.)
拆分地址,得到 ip_list
。跟踪您心中的限制。在转换为整数后增加 ip_list
的每个元素,然后转换回字符串并使用 ".".join(ip_list)
您可以使用 __add__
方法创建 class:
class Ip:
def __init__(self, _ip:str, _stop = 999) -> None:
self.ip = _ip
self.stop = _stop
def __add__(self, _val):
_ip = list(map(int, self.ip.split('.')))
_r = 0
while _r < 4:
result = _ip[3 -_r]+_val
if result < self.stop:
_ip[3 - _r] += _val
break
_ip[3 - _r] += _val%self.stop
_val = _val%self.stop
_r += 1
_ip[4 - _r] = 1
return Ip('.'.join(map(str, _ip)))
def __repr__(self):
return f'<IP {self.ip}>'
p = Ip('192.168.1.1')
new_p = p + 1
print(new_p)
print(p + 1000)
输出:
<IP 192.168.1.2>
<IP 192.168.2.1>
您应该为此使用标准库 ipaddress
。不要尝试用原始字符串操作来完成它,因为有几个陷阱和奇怪的边缘情况。使用面向对象的库将防止您生成无效数据。
迭代从 192.168.1.0 到 192.168.1.255 的 ipv4 地址,等同于迭代 192.168.1.0/24 subnet. Use a network 对象:
>>> from ipaddress import ip_network
>>> list(ip_network('192.168.1.0/24'))
[IPv4Address('192.168.1.0'),
IPv4Address('192.168.1.1'),
IPv4Address('192.168.1.2'),
...
IPv4Address('192.168.1.255')]
其中一些地址不是可用的主机,例如 255 是 broadcast address. Iterate the hosts,如果您要找的是:
>>>> list(ip_network('192.168.1.0/24').hosts())
[IPv4Address('192.168.1.1'),
IPv4Address('192.168.1.2'),
IPv4Address('192.168.1.3'),
...
IPv4Address('192.168.1.254')]
请注意,192.168.1.999 不是有效的 IP 地址,所以不要生成它!验证器无论如何都会阻止你创建它:
>>> from ipaddress import ip_address
>>> ip_address('192.168.1.254')
IPv4Address('192.168.1.254')
>>> ip_address('192.168.1.999')
# ValueError: '192.168.1.999' does not appear to be an IPv4 or IPv6 address
要将 ip address object 转换回普通的旧字符串,只需对其调用 str
。
您的问题还询问了关于 "rolling over" 到 192.168.2.1 的问题。这只是迭代不同的子网。 192.168.1.0/24中的24是指分配给网络前缀的24位有效位(其余8位保留给主机寻址),即子网掩码 255.255.255.0.
要做到 "rollover",您真的只想迭代一个更大的子网:
>>> gen = iter(ip_network('192.168.0.0/16'))
>>> for i in range(255*2):
... next(gen)
...
>>> next(gen)
IPv4Address('192.168.1.254')
>>> next(gen)
IPv4Address('192.168.1.255')
>>> next(gen)
IPv4Address('192.168.2.0')
>>> next(gen)
IPv4Address('192.168.2.1')
要从单个ip地址字符串获取网络对象,可以使用supernet
方法:
>>> from ipaddress import ip_address, ip_interface
>>> ip = ip_address('192.168.1.1')
>>> net = ip_interface(ip).network
>>> net
IPv4Network('192.168.1.1/32')
>>> net.supernet(prefixlen_diff=8)
IPv4Network('192.168.1.0/24')
阅读 Internet 协议版本 4 了解更多信息,以及 Python 的 ipaddress
模块的官方文档。