Python 网络/cidr 计算

标签 python networking scripting cidr

我正在构建一个嵌入式网络设备(基于 Linux)并且遇到了动态构建守护进程配置文件的需要。因此,我需要能够在将构建 conf 文件的 python 代码中进行一些网络地址计算。我不是程序员,所以我担心我编写的模块在设备开始发货后无法像我希望的那样运行。

以下是我目前所拥有的,它是我在这个网站和谷歌上找到的真正拼凑而成的。

是否有更好的方法来查找网络接口(interface)的网络地址和 cidr?将网络掩码转换为 bin str 并计算 1 似乎很不雅观。

import socket
import fcntl
import struct

SIOCGIFNETMASK = 0x891b
SIOCGIFADDR = 0x8915

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def _GetIfaceMask(iface):
    return struct.unpack('L', fcntl.ioctl(s, SIOCGIFNETMASK, struct.pack('256s', iface))[20:24])[0]

def _GetIfaceAddr(iface):
    return struct.unpack('L', fcntl.ioctl(s, SIOCGIFADDR, struct.pack('256s', iface[:15]))[20:24])[0]

def GetIfaceNet(iface):
    net_addr = _GetIfaceAddr(iface) & _GetIfaceMask(iface)
    return socket.inet_ntoa(struct.pack('L', net_addr))

def GetIfaceCidr(iface):
    bin_str = bin(_GetIfaceMask(iface))[2:]
    cidr = 0
    for c in bin_str:
        if c == '1':  cidr += 1
    return cidr

感谢您的任何意见,我对此确实有些迷茫。如果这不是此类反馈的地方,请告诉我。

最佳答案

这可以使用汉明权重算法来解决。从 How to count the number of set bits in a 32-bit integer? 被盗并翻译成 Python:

def number_of_set_bits(x):
    x -= (x >> 1) & 0x55555555
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
    x = ((x >> 4) + x) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f

另一个更具可读性的解决方案(但在 O(log x) 中运行):

def number_of_set_bits(x):
    n = 0
    while x:
        n += x & 1
        x = x >> 1
    return n

关于Python 网络/cidr 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4912523/

相关文章:

c# - 检测目录中的更改

linux - 在 linux bash 脚本中恢复

python - JSON 未正确保存

python - 无法从 celery 信号连接到 celery 任务?

python - 无法使用 scrapy 使用此代码提取任何数据

amazon-web-services - 为什么子网不能跨越 AWS 中的可用区?

python - 对于其他列中的每次更改,我们如何用这些行的平均值替换多行数据?

java - 多人游戏客户端/服务器架构 RMI/JMS/Sockets

networking - 位填充示例

PHP while 循环加 2