为什么 %en0 后缀无法在 Python 中连接本地链路 IPv6 TCP 套接字?

发布于 2024-09-28 21:09:01 字数 1105 浏览 6 评论 0原文

大约一周前,StackOverflow 上有人为什么他们的 Python连接到 IPv6 链路本地地址的代码不起作用,我回答说,因为它是链路本地地址,所以他们需要向目标添加 %en0 (或任何所需的本地接口名称)后缀IP 地址。我以为我知道我在说什么,所以在回答之前我并没有真正测试我的建议(真丢脸!)。

今天我自己也尝试使用同样的技术,却发现它似乎不起作用。 :^( 也就是说,这段代码不起作用:

>>> from socket import *
>>> s = socket(AF_INET6, SOCK_STREAM)
>>> s.connect(('fe80::21f:5bff:fe3f:1b36%en0', 2001))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 65] No route to host

另一方面,下面的代码可以工作(有或没有 %en0 后缀):

>>> from socket import *
>>> s = socket(AF_INET6, SOCK_STREAM)
>>> s.connect(('fe80::21f:5bff:fe3f:1b36%en0', 2001, 0, 6))
>>> 

...但我不喜欢这样做,因为为了为了找出为最后一个参数提供哪个作用域 ID 整数,我必须执行一堆不太可移植的代码来迭代本地接口列表,找到名为“en0”的接口,并提取其作用域 ID,其中 FWIW

鉴于 connect() 接受 IP 地址的 %en0 后缀,为什么它实际上没有按预期使用它来确定范围 ID

,我正在测试 ? MacOS/X 10.6.4 下的 Python 2.6.1。

A week or so ago someone on StackOverflow asked why their Python code for connecting to an IPv6 link-local address wasn't working, and I replied that since it was a link-local address they needed to add a %en0 (or whatever the desired local-interface-name is) suffix to their target IP address. I thought I knew what I was talking about, so I didn't actually test my suggestion before answering (shame on me!).

Today I went to use that same technique for myself, only to find that it doesn't seem to work. :^( That is, this code does not work:

>>> from socket import *
>>> s = socket(AF_INET6, SOCK_STREAM)
>>> s.connect(('fe80::21f:5bff:fe3f:1b36%en0', 2001))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 65] No route to host

The following code, on the other hand, DOES work (with or without the %en0 suffix):

>>> from socket import *
>>> s = socket(AF_INET6, SOCK_STREAM)
>>> s.connect(('fe80::21f:5bff:fe3f:1b36%en0', 2001, 0, 6))
>>> 

... but I don't like doing it that way, because in order to figure out which scope ID integer to supply for the last argument, I have to execute a bunch of not-very-portable code to iterate over the local interfaces list, find the interface named 'en0', and extract its scope ID, which is more complexity overhead than I'd like to have.

Given that connect() is accepting the %en0 suffix to the IP address, why isn't it actually using it as expected to determine the scope ID?

FWIW, I am testing with Python 2.6.1 under MacOS/X 10.6.4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2024-10-05 21:09:01

这是进行 ipv6 连接的正确方法:

>>> addrinfo = getaddrinfo('fe80::225:ff:fecd:5aa0%en0', 2001, AF_INET6, SOCK_STREAM)
>>> addrinfo
[(30, 1, 6, '', ('fe80::225:ff:fecd:5aa0%en0', 2001, 0, 4))]
>>> (family, socktype, proto, canonname, sockaddr) = addrinfo[0]
>>> s = socket(family, socktype, proto)
>>> s.connect(sockaddr)

getaddrinfo() 将为您返回正确的数字范围和流量信息。

This is the correct way to do an ipv6 connection:

>>> addrinfo = getaddrinfo('fe80::225:ff:fecd:5aa0%en0', 2001, AF_INET6, SOCK_STREAM)
>>> addrinfo
[(30, 1, 6, '', ('fe80::225:ff:fecd:5aa0%en0', 2001, 0, 4))]
>>> (family, socktype, proto, canonname, sockaddr) = addrinfo[0]
>>> s = socket(family, socktype, proto)
>>> s.connect(sockaddr)

getaddrinfo() will return the correct numerical scope and flow information for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文