Python CDLL.freeifaddrs方法代码示例

本文整理汇总了Python中ctypes.CDLL.freeifaddrs方法的典型用法代码示例。如果您正苦于以下问题:Python CDLL.freeifaddrs方法的具体用法?Python CDLL.freeifaddrs怎么用?Python CDLL.freeifaddrs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ctypes.CDLL的用法示例。


Python CDLL.freeifaddrs方法代码示例

在下文中一共展示了CDLL.freeifaddrs方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: get_ifaddrs

# 需要导入模块: from ctypes import CDLL [as 别名]
# 或者: from ctypes.CDLL import freeifaddrs [as 别名]

#.........这里部分代码省略.........
            ("sdl_data", (c_uint8 * 46))
        ]

    if platform.startswith("darwin"):
        libc = CDLL("libSystem.dylib")

    elif platform.startswith("freebsd"):
        libc = CDLL("libc.so")

    else:
        libc = CDLL("libc.so.6")

    ptr = c_void_p(None)
    result = libc.getifaddrs(pointer(ptr))
    if result:
        return None
    ifa = ifaddrs.from_address(ptr.value)
    result = []

    while ifa:
        # Python 2 gives us a string, Python 3 an array of bytes
        if type(ifa.ifa_name) is str:
            name = ifa.ifa_name
        else:
            name = ifa.ifa_name.decode()

        if ifa.ifa_addr:
            sa = sockaddr.from_address(ifa.ifa_addr)

            data = {}

            if sa.sa_family == AF_INET:
                if ifa.ifa_addr is not None:
                    si = sockaddr_in.from_address(ifa.ifa_addr)
                    data['addr'] = inet_ntop(AF_INET, si.sin_addr)

                if ifa.ifa_netmask is not None:
                    si = sockaddr_in.from_address(ifa.ifa_netmask)
                    data['netmask'] = inet_ntop(AF_INET, si.sin_addr)

                # check if a valid broadcast address is set and retrieve it
                # 0x2 == IFF_BROADCAST
                if ifa.ifa_flags & 0x2:
                    si = sockaddr_in.from_address(ifa.ifa_ifu.ifu_broadaddr)
                    data['broadcast'] = inet_ntop(AF_INET, si.sin_addr)

            if sa.sa_family == AF_INET6:
                if ifa.ifa_addr is not None:
                    si = sockaddr_in6.from_address(ifa.ifa_addr)
                    data['addr'] = inet_ntop(AF_INET6, si.sin6_addr)

                    if data['addr'].startswith('fe80:'):
                        data['scope'] = si.sin6_scope_id

                if ifa.ifa_netmask is not None:
                    si = sockaddr_in6.from_address(ifa.ifa_netmask)
                    data['netmask'] = inet_ntop(AF_INET6, si.sin6_addr)

            if sa.sa_family == AF_PACKET:
                if ifa.ifa_addr is not None:
                    si = sockaddr_ll.from_address(ifa.ifa_addr)
                    addr = ""
                    total = 0
                    for i in range(si.sll_halen):
                        total += si.sll_addr[i]
                        addr += "%02x:" % si.sll_addr[i]
                    addr = addr[:-1]
                    if total > 0:
                        data['addr'] = addr

            if sa.sa_family == AF_LINK:
                dl = sockaddr_dl.from_address(ifa.ifa_addr)

                if dl.sdl_type == IFT_ETHER:
                    addr = ""
                    for i in range(dl.sdl_alen):
                        addr += "%02x:" % dl.sdl_data[dl.sdl_nlen + i]

                    addr = addr[:-1]
                    data['addr'] = addr

            if len(data) > 0:
                iface = {}
                for interface in result:
                    if name in interface.keys():
                        iface = interface
                        break
                if iface:
                    iface[name][sa.sa_family] = data
                else:
                    iface[name] = { sa.sa_family : data }
                    result.append(iface)

        if ifa.ifa_next:
            ifa = ifaddrs.from_address(ifa.ifa_next)
        else:
            break

    libc.freeifaddrs(ptr)
    return result
开发者ID:Alidron,项目名称:alidron-isac,代码行数:104,代码来源:zhelper.py

示例2: getifaddrs

# 需要导入模块: from ctypes import CDLL [as 别名]
# 或者: from ctypes.CDLL import freeifaddrs [as 别名]

#.........这里部分代码省略.........
        _fields_ = [("u6_addr8", c_uint8 * 16), ("u6_addr16", c_uint16 * 8), ("u6_addr32", c_uint32 * 4)]

    class in6_addr(Union):
        _fields_ = [("in6_u", in6_u)]

    class sockaddr_in6(Structure):
        _fields_ = [
            ("sin6_family", c_short),
            ("sin6_port", c_ushort),
            ("sin6_flowinfo", c_uint32),
            ("sin6_addr", in6_addr),
            ("sin6_scope_id", c_uint32),
        ]

    # AF_PACKET / Linux
    class sockaddr_ll(Structure):
        _fields_ = [
            ("sll_family", c_uint16),
            ("sll_protocol", c_uint16),
            ("sll_ifindex", c_uint32),
            ("sll_hatype", c_uint16),
            ("sll_pktype", c_uint8),
            ("sll_halen", c_uint8),
            ("sll_addr", c_uint8 * 8),
        ]

    # AF_LINK / BSD|OSX
    class sockaddr_dl(Structure):
        _fields_ = [
            ("sdl_len", c_uint8),
            ("sdl_family", c_uint8),
            ("sdl_index", c_uint16),
            ("sdl_type", c_uint8),
            ("sdl_nlen", c_uint8),
            ("sdl_alen", c_uint8),
            ("sdl_slen", c_uint8),
            ("sdl_data", c_uint8 * 46),
        ]

    families = {AF_INET: "ipv4", AF_INET6: "ipv6", AF_PACKET: "hw"}

    libc = CDLL("libc.so.6")
    ptr = c_void_p(None)
    result = libc.getifaddrs(pointer(ptr))
    if result:
        return None
    ifa = ifaddrs.from_address(ptr.value)
    result = {}

    while ifa:
        name = ifa.ifa_name
        #       name = ifa.ifa_name.decode('UTF-8') # use this for python3

        if name not in result:
            result[name] = {}

        sa = sockaddr.from_address(ifa.ifa_addr)

        if sa.sa_family not in result[name]:
            result[name][families[sa.sa_family]] = []

        data = {}

        if sa.sa_family == AF_INET:
            if ifa.ifa_addr is not None:
                si = sockaddr_in.from_address(ifa.ifa_addr)
                data["addr"] = inet_ntop(si.sin_family, si.sin_addr)
            if ifa.ifa_netmask is not None:
                si = sockaddr_in.from_address(ifa.ifa_netmask)
                data["netmask"] = inet_ntop(si.sin_family, si.sin_addr)

        if sa.sa_family == AF_INET6:
            if ifa.ifa_addr is not None:
                si = sockaddr_in6.from_address(ifa.ifa_addr)
                data["addr"] = inet_ntop(si.sin6_family, si.sin6_addr)
                if data["addr"].startswith("fe80:"):
                    data["scope"] = si.sin6_scope_id
            if ifa.ifa_netmask is not None:
                si = sockaddr_in6.from_address(ifa.ifa_netmask)
                data["netmask"] = inet_ntop(si.sin6_family, si.sin6_addr)

        if sa.sa_family == AF_PACKET:
            if ifa.ifa_addr is not None:
                si = sockaddr_ll.from_address(ifa.ifa_addr)
                addr = ""
                for i in range(si.sll_halen):
                    addr += "%02x:" % si.sll_addr[i]
                addr = addr[:-1]
                data["addr"] = addr

        if len(data) > 0:
            result[name][families[sa.sa_family]].append(data)

        if ifa.ifa_next:
            ifa = ifaddrs.from_address(ifa.ifa_next)
        else:
            break

    libc.freeifaddrs(ptr)
    return result
开发者ID:exekias,项目名称:droplet,代码行数:104,代码来源:util.py

本文标签属性:

示例:示例是什么意思

代码:代码编程

Python:python什么意思

CDLL:CDLL

上一篇:之光是什么意思(艾青诗选中的东方黎明的光茫是什么之光)
下一篇:Java TimePeriod类代码示例

为您推荐