I’m trying to enable IPv6 in a Python 2 application and am running into trouble. Whenever I try to bind to an IPv6 socket, a socket.error: getsockaddrarg: bad family exception is thrown. I can reproduce the error simply by doing:
import socket
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.bind(('', 12345))
This code works fine if I run in Python 3. Unfortunately the script would need a significant porting effort to work in Python 3 and I’d rather not have to do that yet.
Is there something I need to do to get IPv6 working in Python 2 or am I S-O-L?
Details:
Python 2.6.2 (r262:71600, Oct 24 2009, 03:16:31)
[GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2
(it’s the Python that’s part of the standard openSUSE 11.2 install).
Update
After AndiDog helped me figure out that socket.AF_INET6 is defined even when IPv6 is not configured, I discovered socket.has_ipv6. This is defined as a boolean and indicates whether Python was build with IPv6.
Okay here’s the answer from the comments:
Seems like Python wasn’t configured with
--enable-ipv6.It shouldn’t be a OS problem because Python 3 works. Even if the OS doesn’t have IPv6 support, it seems that
socket.AF_INET6is always available (if it is defined in the OS header files). Cf. socketmodule.c, line 4433 (in current Python 2.6.4 source code).