Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2 and IPv6

Tags:

python

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.

like image 879
R Samuel Klatchko Avatar asked Jan 15 '10 23:01

R Samuel Klatchko


3 Answers

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).

like image 105
AndiDog Avatar answered Nov 03 '22 16:11

AndiDog


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.

like image 30
Andrew McGregor Avatar answered Nov 03 '22 17:11

Andrew McGregor


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)...

like image 30
Alex Martelli Avatar answered Nov 03 '22 18:11

Alex Martelli