python中的getaddrinfo失败

时间:2014-04-02 06:19:54

标签: python network-programming getaddrinfo

我的python脚本使用getaddrinfo()来解析地址,然后才能'绑定()'。

剧本片段:

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)

脚本在执行时抛出错误:

    # ./ip6_l2tp_ip.py
Traceback (most recent call last):
  File "./ip6_l2tp_ip.py", line 36, in <module>
    sockaddr = fetch_ipv6_address("::1")
  File "./ip6_l2tp_ip.py", line 26, in fetch_ipv6_address
    addrs = socket.getaddrinfo(addr, 0, socket.AF_INET6, socket.SOCK_RAW, 0x73, socket.AI_PASSIVE)
socket.gaierror: [Errno -8] Servname not supported for ai_socktype

任何关于getaddrinfo()args有什么问题的想法?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

OTOH,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,则可以。