这个问题有两个部分
您应该使用sa.bind(sockaddr),其中从getaddrinfo获得sockaddr
>>> 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)
如果您查看套接字文档中提供的示例,请访问
套接字接受三个参数
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对我来说是正确的。