【问题标题】:Find IPs From Ranges In Python [duplicate]在 Python 中从范围中查找 IP [重复]
【发布时间】:2020-11-09 16:25:07
【问题描述】:

我正在尝试使用以下代码从范围(172.16.3.20 到 192.168.1.1)中获取 ip,但它需要大量时间和 cpu,有时它根本不起作用。有没有更好的方法找到它?所需输出如下:
172.16.3.21
172.16.3.22 ...
...
192.167.255.255
192.168.1.1

代码:

start = list(map(int, start_ip.split(".")))  
end = list(map(int, end_ip.split(".")))   
ip_range = []  
temp = start  
ip_range.append(start_ip)  
while temp != end:    
    start[3] += 1  
    for i in (3, 2, 1):
       if temp[i] == 256:
          temp[i] = 0
          temp[i-1] += 1
    ip_range.append(".".join(map(str, temp)))

【问题讨论】:

标签: python python-3.x ip


【解决方案1】:

给你:

from ipaddress import IPv4Address

start = IPv4Address('172.16.3.20') + 1
end = IPv4Address('192.168.1.1')
ips = []
while start <= end:
    ips.append(start)
    start += 1

【讨论】:

  • 它按照你的建议工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
相关资源
最近更新 更多