Python中的getaddrinfo失败

2024-09-26 17:50:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python脚本使用getaddrinfo()来解析地址,然后才能对其进行“bind()”。在

脚本片段:

def fetch_ipv6_address(addr="::1"):
    # try to detect whether IPv6 is supported at the present system and
    # fetch the IPv6 address of localhost.
    if not socket.has_ipv6:
        raise Exception("the local machine has no IPv6 support enabled")

    addrs = socket.getaddrinfo(addr, 0, socket.AF_INET6, socket.SOCK_RAW, 0x73, socket.AI_PASSIVE)
    ....
    ....

sockaddr = fetch_ipv6_address("::1")
RX = socket.socket(socket.AF_INET6, socket.SOCK_RAW, 0x73)
RX.bind(sockaddr)

脚本在执行时引发错误:

^{pr2}$

知道getaddrinfo()参数有什么问题吗?在

谢谢!在


Tags: the脚本rawbindaddresssocketfetchsock
1条回答
网友
1楼 · 发布于 2024-09-26 17:50:57

如果字符串是long或int,则将0作为第二个参数is converted,以便它适合底层API调用支持的ai_servname字段的格式。在

奥托,the docs写下来

o   For internet address families, if you specify servname while you set
    ai_socktype to SOCK_RAW, getaddrinfo() will raise an error, because
    service names are not defined for the internet SOCK_RAW space.

如果将0替换为None,它就可以工作了。在

相关问题 更多 >

    热门问题