tags:

views:

227

answers:

3
+1  Q: 

Python 2 and IPv6

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.

A: 

Works fine with 2.6.4 on my Mac (Mac OS X 10.5.8) -- and unfortunately I can't downgrade to 2.6.2 nor do I have any openSUSE around to check where the bug specifically comes from you. Could you try getting 2.6.4 and building from sources to see if the bug goes away, or check some openSUSE-specific bug tracker...? At least we do know it's not a generic Python 2.6 bug (with the latest, bug-fixed version of 2.6, at least)...

Alex Martelli
A: 

Sounds like that particular Python was not compiled with IPv6 support.

In which case, you can download the source for that version and build yourself a compatible Python that will work. You may even be able to do some editing in the Debian package and upgrade the system python.

Andrew McGregor
+1  A: 

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_INET6 is always available (if it is defined in the OS header files). Cf. socketmodule.c, line 4433 (in current Python 2.6.4 source code).

AndiDog
Thanks. I assumed that the presence of socket.AF_INET6 indicated that IPv6 was enabled.
R Samuel Klatchko