我有一系列的IP地址:
1.48.0.0 - 1.51.255.255
如何获取IP地址列表?
答案 0 :(得分:2)
如果我理解正确的话......
start = [1,48,0,0]
end = [1,51,255,255]
def generate_range(start, end):
cur = start
while cur < end:
cur[3] = int(cur[3]) + 1
for pos in range(len(cur)-1, -1, -1):
if cur[pos] == 255:
cur[pos] = 0
cur[pos-1] = int(cur[pos-1]) + 1
yield '.'.join("{}".format(cur[i]) for i in range(0,len(cur)))
for x in generate_range(start, end):
print (x)
很难看,但会完成这项工作
这将创建一个包含所有可能的ip
值的生成器序列。
请注意,这是python 3.0
代码,为了获得最佳结果,请使用xrange
中的python 2.X
编辑: 该算法的最新版本有一个错误,此版本没有
答案 1 :(得分:2)
from struct import *
from socket import *
for ip in xrange(unpack('!I',inet_pton(AF_INET,"1.47.0.0"))[0],unpack('!I',inet_pton(AF_INET,"1.51.255.255"))[0]):
print inet_ntop(AF_INET,pack('!I',ip));
f = unpack('!I',inet_pton(AF_INET,"1.47.0.0"))[0]
l = unpack('!I',inet_pton(AF_INET,"1.51.255.255"))[0]
while f < l:
print inet_ntop(AF_INET,pack('!I',f));
f = f + 1
通过这种方式,通过IPv6地址也很容易,但是由于IPv6空间的广泛,我不会推荐它。
答案 2 :(得分:0)
我使用简单的嵌套while循环实现了它。 如果您发现任何错误,请提示我。
a=1
b=48
while b <= 51:
c=0
d=0
while c <= 255:
d=0
while d <= 255:
print str(a)+"."+str(b)+"."+str(c)+"."+str(d)
d += 1
c += 1
b += 1
答案 3 :(得分:0)
您可以将此代码扩展为包含4个字段的IP表单。
现在您可以使用ipRange*
startIP='0.0'
endIP = '10.10'
ipRange = []
for i in range(int(startIP.split('.')[-1]), int(endIP.split('.')[-1])):
for j in range(int(startIP.split('.')[-2]), int(endIP.split('.')[-2])):
ipRange.append(str(i)+ ''.join('.') + str(j))
结果是:
$ ipRange
['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1.0', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '1.9', '2.0', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '2.8', '2.9', '3.0', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8', '3.9', '4.0', '4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5', '5.6', '5.7', '5.8', '5.9', '6.0', '6.1', '6.2', '6.3', '6.4', '6.5', '6.6', '6.7', '6.8', '6.9', '7.0', '7.1', '7.2', '7.3', '7.4', '7.5', '7.6', '7.7', '7.8', '7.9', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5', '8.6', '8.7', '8.8', '8.9', '9.0', '9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '9.7', '9.8', '9.9']
答案 4 :(得分:0)
获取给定范围内的所有有效IP地址
我会使用专门为处理IP地址而编写的模块。 netaddr ( pip install netaddr )提供了一个简单的解决方案。这是一个打印给定范围内所有IP地址的示例(例如1.48.0.0 - 1.51.255.255):
#!/usr/bin/env python
"""Get a list of IP Addresses in a range (CIDR Notation)."""
import sys # Using for cleanly exiting program.
try:
# Pythonic manipulation of IPv4, IPv6, CIDR, EUI and MAC network addresses.
from netaddr import IPNetwork
except ImportError:
sys.exit(
"""
Missing 'netaddr' module, get it here:
https://pypi.python.org/pypi/netaddr
"""
)
def get_ips(cidr_string):
"""Returns all IPv4 Addresses in a given CIDR Range
:param cidr_string: IP Address Range in CIDR notation
:returns: list of IP Addresses
:rtype: list
"""
# Cast to list for simpler manipulation.
ip_list = list(IPNetwork(cidr_string))
return ip_list
# Get IPs in Range '1.48.0.0 - 1.51.255.255' (Note the CIDR Notation).
ip_list = get_ips('1.48.0.0/14')
for ip_address in ip_list:
print ip_address
答案 5 :(得分:0)
我这样做(在python3中):
encode()