gpt4 book ai didi

python - python ipaddress.IPv6Address 实例中的范围 ID

转载 作者:行者123 更新时间:2023-12-05 07:55:57 25 4
gpt4 key购买 nike

我目前正在为 C++ 库编写一个 python 包装器,它通过 IPv6 链接本地地址进行网络通信。要初始化库,必须选择网络接口(interface)。这是通过所需接口(interface)的链路本地地址完成的。

在 C++ 方面,我们使用 boost address_v6 objects .在 python 端 ipaddress.IPv6Address似乎是绝配。

但是我没有找到在 python 端指定接口(interface)范围 id 的方法。

ip = ipaddress.IPv6Address('fe80::3c4e:6a7:3f57:4cbc%12')

给出错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\ipaddress.py", line 1835, in __init__
self._ip = self._ip_int_from_string(addr_str)
File "C:\Python34\lib\ipaddress.py", line 1664, in _ip_int_from_string
raise AddressValueError("%s in %r" % (exc, ip_str)) from None
ipaddress.AddressValueError: Only hex digits permitted in '4cbc%12' in 'fe80::3c4e:6a7:3f57:4cbc%12'

如果没有范围 ID,IPv6 链路本地通信几乎是不可能的。

我是否遗漏了什么,或者是将 IPv6 链路本地接口(interface)地址指定为字符串的最佳方式吗?

最佳答案

我建议在所有有意义的情况下使用与 Python 套接字模块相同的结构。即使使用您的 native 库意味着不会使用 Python 套接字模块,但如果某些数据结构在两者之间兼容,它仍然是有益的。

为了表示 IP 地址,Python 套接字模块确实使用了字符串。该字符串可以在适当的时候包含范围 ID。

为此,Python 套接字模块还需要表示套接字地址,它使用由四个字段组成的元组。例如,它可以是:

('ff02::2%eth0', 80, 0, 2)

请注意,此表示有点多余,因为范围 ID 在 IP 地址内指定为 %eth02(元组的最后一个元素) .

关于python - python ipaddress.IPv6Address 实例中的范围 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com