--- test_urllib2.py 2007-09-25 09:06:03.704300840 +0530 +++ test_urllib2-getaddrinfo.py 2007-09-25 09:05:21.118774824 +0530 @@ -540,6 +540,23 @@ def sanepathname2url(path): # XXX don't ask me about the mac... return urlpath +def gethost_addrinfo(hostname): + if socket.has_ipv6: + try: + for res in socket.getaddrinfo(hostname, None, socket.AF_INET6, + socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME): + af, socktype, proto, canonname, sa = res + except socket.gaierror: + for res in socket.getaddrinfo(hostname, None, socket.AF_INET, + socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME): + af, socktype, proto, canonname, sa = res + else: + for res in socket.getaddrinfo(hostname, None, socket.AF_INET, + socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME): + af, socktype, proto, canonname, sa = res + + return sa[0] + class HandlerTests(unittest.TestCase): def test_ftp(self): @@ -579,7 +596,7 @@ class HandlerTests(unittest.TestCase): r = h.ftp_open(req) # ftp authentication not yet implemented by FTPHandler self.assert_(h.user == h.passwd == "") - self.assertEqual(h.host, socket.gethostbyname(host)) + self.assertEqual(h.host, gethost_addrinfo(host)) self.assertEqual(h.port, port) self.assertEqual(h.dirs, dirs) self.assertEqual(h.ftpwrapper.filename, filename) @@ -599,10 +616,10 @@ class HandlerTests(unittest.TestCase): urls = [ "file://localhost%s" % urlpath, "file://%s" % urlpath, - "file://%s%s" % (socket.gethostbyname('localhost'), urlpath), + "file://%s%s" % (gethost_addrinfo('localhost'), urlpath), ] try: - localaddr = socket.gethostbyname(socket.gethostname()) + localaddr = gethost_addrinfo(socket.gethostname()) except socket.gaierror: localaddr = '' if localaddr: @@ -635,7 +652,7 @@ class HandlerTests(unittest.TestCase): for url in [ "file://localhost:80%s" % urlpath, "file:///file_does_not_exist.txt", - "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), + "file://%s:80%s/%s" % (gethost_addrinfo('localhost'), os.getcwd(), TESTFN), "file://somerandomhost.ontheinternet.com%s/%s" % (os.getcwd(), TESTFN),