问题有两个部分
首要问题
您应该使用 sa.bind(sockaddr) ,其中 sockaddr 是从 getaddrinfo 获取的
>>> HOST = 'localhost'
>>> PORT = 50007
>>> res = socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_DGRAM, 0, socket.AI_PASSIVE)
>>> family, socktype, proto, canonname, sockaddr = res[1]
>>> proto
17
>>> sockaddr
('fe80::1%lo0', 50007, 0, 1)
第二期
如果您查看套接字文档中提供的示例,请访问
- http://docs.python.org/release/2.5.2/lib/module-socket.html
套接字接受三个参数
socket( [family[, type[, proto]]])
根据文档
Create a new socket using the given address family,
socket type and protocol number. The address family
should be AF_INET (the default), AF_INET6 or AF_UNIX.
The socket type should be SOCK_STREAM (the default),
SOCK_DGRAM or perhaps one of the other "SOCK_" constants.
The protocol number is usually zero and may be omitted in that case.
如果您使用 getaddressinfo 来获取 proto 的值,则该值与默认的 0 不同
但是当我执行以下命令时,我得到了不同的协议值 - 17。
您可能也想对此进行调查。
当然,socket.has_ipv6 对我来说是 True 。