def test__inet_pton_win(self): if _inet_pton_win != inet_pton: raise SkipTest('socket.inet_pton is available, no need to test') # only test what we need this function for self.assertEqual(inet_pton(socket.AF_INET, '192.168.1.1'), '\xc0\xa8\x01\x01') self.assertRaises(socket.error, inet_pton, socket.AF_INET, 'foo') self.assertEqual(inet_pton(socket.AF_INET6, '::1'), '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01') self.assertRaises(socket.error, inet_pton, socket.AF_INET6, 'foo')
def test__inet_pton_win(self): if _inet_pton_win != inet_pton: raise SkipTest('socket.inet_pton is available, no need to test') # only test what we need this function for self.assertEqual(inet_pton(socket.AF_INET, '192.168.1.1'), '\xc0\xa8\x01\x01') self.assertRaises(socket.error, inet_pton, socket.AF_INET, 'foo') self.assertEqual( inet_pton(socket.AF_INET6, '::1'), '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01') self.assertRaises(socket.error, inet_pton, socket.AF_INET6, 'foo')
def mapto_v6(addr): """ Map an IPv4 address to an IPv6 one. If the address is already an IPv6 one, just return it. Return None if the IP address is not valid. """ try: inet_pton(socket.AF_INET, addr) return '::ffff:{}'.format(addr) except socket.error: try: inet_pton(socket.AF_INET6, addr) return addr except socket.error: log.debug('%s is not a valid IP address.', addr) return None