Python 2 and IPv6

I am trying to include IPv6 in a Python 2 application and I am having problems. Whenever I try to bind to an IPv6 socket, a socket.error: getsockaddrarg: bad family exception is socket.error: getsockaddrarg: bad family . I can reproduce the error simply:

 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 will require significant porting work to work in Python 3, and I would rather not do it yet.

Is there something I need to do to make IPv6 work in Python 2 or am I SOL?

More: Python 2.6.2 (r262: 71600, October 24, 2009, 03:16:31) [GCC 4.4.1 [gcc-4_4-revision branch 150839]] on linux2 (this is Python, which is part of the standard openSUSE 11.2 installation) .

Update

After AndiDog helped me figure out that socket.AF_INET6 was detected, even if IPv6 was not configured, I discovered socket.has_ipv6 . This is defined as logical and indicates whether Python was created with IPv6.

+4
source share
3 answers

Ok, here is the answer from the comments:

It looks like Python was not configured with --enable-ipv6 .

This should not be an OS problem because Python 3 works. Even if the OS does not support IPv6, it seems that socket.AF_INET6 always available (if it is defined in the OS header files). Cp socketmodule.c, line 4433 (in the current Python 2.6.4 source code).

+4
source

It seems that this particular Python has not been compiled with IPv6 support.

In this case, you can download the source code for this version and create yourself a compatible Python that will work. You can even make some changes to the Debian package and update system python.

+3
source

Works well with 2.6.4 on my Mac (Mac OS X 10.5.8) - and, unfortunately, I can't downgrade to 2.6.2, and I don't have openSUSE to check exactly where the error is from you. Could you try to get 2.6.4 and build from sources to find out if the error has disappeared or check some kind of error debugger in openSUSE ...? At least we know that this is not a general Python 2.6 bug (with the latest 2.6 bug fix, at least) ...

+2
source

All Articles