您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在python中创建IPv6套接字?为什么会出现socket.error:(22,'Invalid arguments')?

如何在python中创建IPv6套接字?为什么会出现socket.error:(22,'Invalid arguments')?

这个问题有两个部分

您应该使用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对我来说是正确的。

python 2022/1/1 18:39:40 有270人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶