The getaddrinfo()
function not only allows for client programs to efficiently find the correct data for creating a socket to a given host, it also allows for se
JFTR: It seems now that the program given in the manpage is wrong.
There are two possible approaches for listening to both IP types:
Create only a IPv6 socket and switch off the v6 only flag:
from socket import *
s = socket(AF_INET6, SOCK_STREAM)
s.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
s.bind(...)
resp.
from socket import *
ai = getaddrinfo(None, ..., AF_INET6, SOCK_STREAM, 0, AI_PASSIVE)[0]
s = socket(ai[0], ai[1], ai[2])
s.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
s.bind(ai[4])
Pros:
Cons:
work with two sockets and switch on the v6only flag:
from socket import *
aii = getaddrinfo(None, ..., AF_UNSPEC, SOCK_STREAM, 0, AI_PASSIVE)
sl = []
for ai in aii:
s = socket(ai[0], ai[1], ai[2])
if ai[0] == AF_INET6: s.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 1)
s.bind(ai[4])
sl.append(s)
and handle all sockets in sl
in accepting loop (use select()
or nonblocking IO to do so)
Pros:
getaddrinfo()
Cons: