2014-04-02 146 views
1

我的python腳本使用getaddrinfo()來解析地址,然後它可以'bind()'到它。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任何想法?

謝謝!

+0

什麼是0x73這裏? – glglgl

回答

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. 

如果更換0None,它的工作原理。

+0

工作。謝謝! – Maddy

相關問題