0

我在 csv 文件中有一组带有标签描述的子网。我需要将这些描述分配给另一个 csv 文件中这些子网所属的 Data Probe 范围。

给定一个带有 ipaddress34.0.0.0和 netmask的子网255.255.0.0
我想检查子网是否在范围内34.163.83.230-34.163.83.230

我考虑过从子网的 ip 和网络掩码创建一个范围,并将其与 Data Probe 范围进行比较。我无法确定这是否会产生正确的答案。

我不能使用最新版本的 Python(这必须与运行 python 2.7 的应用程序一起使用),所以该ipaddress模块不适合我。

4

1 回答 1

1

The socket module provides inet_aton, which will convert your addresses to bitstrings. You can then convert them to integers using struct.unpack, mask using &, and use integer comparison:

from socket import inet_aton
from struct import unpack

def atol(a):
    return unpack(">L", inet_aton(a))[0]

addr = atol("30.44.230.0")
mask = atol("255.255.0.0")
lo = atol("32.44.230.0")
hi = atol("32.44.230.255")
prefix = addr & mask

print lo <= prefix <= hi 
于 2014-03-12T22:08:14.700 回答