There's no guarantee that there are exactly two results.  Not all systems are configured for ipv6, and on some systems 'localhost' resolves to more than two addresses (on a mac I get ::1, 127.0.0.1, and fe80::1%lo0).  Maybe just test that `all(s.getsockname()[1] == sockets[0].getsockname()[1] for s in sockets)`?  It will be a meaningless test on non-ipv6 systems but that's OK because the problem doesn't exist there.

This is also an annoying test to run on a mac since binding to 'localhost' on ipv6 for some reason results in a firewall prompt that doesn't happen when using ipv4.  I'm not sure if there's a good way around that.