--- python-trunk/Lib/urllib2.py.ipv6 2007-03-05 17:58:36.000000000 -0500 +++ python-trunk/Lib/urllib2.py 2007-03-07 00:05:46.000000000 -0500 @@ -1200,14 +1200,43 @@ # names for the localhost names = None def get_names(self): + def make_host_tuple(searchlist): + names = [] + + for hostname in searchlist: + for res in socket.getaddrinfo(host=hostname, port=None, + flags=socket.AI_CANONNAME): + af, socktype, proto, canonname, sa = res + try: + i = names.index(canonname) + except ValueError: + names.append(canonname) + + return tuple(names) + + if socket.has_ipv6: + searchlist = ['::1', 'localhost'] + else: + searchlist = ['localhost'] + if FileHandler.names is None: try: - FileHandler.names = (socket.gethostbyname('localhost'), - socket.gethostbyname(socket.gethostname())) + searchlist.append(socket.gethostname()) + FileHandler.names = make_host_tuple(searchlist) except socket.gaierror: - FileHandler.names = (socket.gethostbyname('localhost'),) + FileHandler.names = make_host_tuple(searchlist) return FileHandler.names + # check for given host name in local host name list + def check_host(self, host): + locals = self.get_names() + for res in socket.getaddrinfo(host=host, port=None, + flags=socket.AI_CANONNAME): + af, socktype, proto, canonname, sa = res + if canonname in locals: + return True + return False + # not entirely sure what the rules are here def open_local_file(self, req): import email.utils @@ -1224,8 +1253,7 @@ (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) - if not host or \ - (not port and socket.gethostbyname(host) in self.get_names()): + if not host or (not port and self.check_host(host)): return addinfourl(open(localfile, 'rb'), headers, 'file:'+file) raise URLError('file not on local host') @@ -1237,6 +1265,7 @@ host = req.get_host() if not host: raise IOError, ('ftp error', 'no host given') + host, port = splitport(host) if port is None: port = ftplib.FTP_PORT @@ -1253,36 +1282,41 @@ user = unquote(user or '') passwd = unquote(passwd or '') - try: - host = socket.gethostbyname(host) - except socket.error, msg: - raise URLError(msg) path, attrs = splitattr(req.get_selector()) dirs = path.split('/') dirs = map(unquote, dirs) dirs, file = dirs[:-1], dirs[-1] if dirs and not dirs[0]: dirs = dirs[1:] + try: - fw = self.connect_ftp(user, passwd, host, port, dirs) - type = file and 'I' or 'D' - for attr in attrs: - attr, value = splitvalue(attr) - if attr.lower() == 'type' and \ - value in ('a', 'A', 'i', 'I', 'd', 'D'): - type = value.upper() - fp, retrlen = fw.retrfile(file, type) - headers = "" - mtype = mimetypes.guess_type(req.get_full_url())[0] - if mtype: - headers += "Content-type: %s\n" % mtype - if retrlen is not None and retrlen >= 0: - headers += "Content-length: %d\n" % retrlen - sf = StringIO(headers) - headers = mimetools.Message(sf) - return addinfourl(fp, headers, req.get_full_url()) - except ftplib.all_errors, msg: - raise IOError, ('ftp error', msg), sys.exc_info()[2] + for res in socket.getaddrinfo(host=host, port=None, + flags=socket.AI_CANONNAME): + af, socktype, proto, host, sa = res + + try: + fw = self.connect_ftp(user, passwd, host, port, dirs) + type = file and 'I' or 'D' + for attr in attrs: + attr, value = splitvalue(attr) + if attr.lower() == 'type' and \ + value in ('a', 'A', 'i', 'I', 'd', 'D'): + type = value.upper() + fp, retrlen = fw.retrfile(file, type) + headers = "" + mtype = mimetypes.guess_type(req.get_full_url())[0] + if mtype: + headers += "Content-type: %s\n" % mtype + if retrlen is not None and retrlen >= 0: + headers += "Content-length: %d\n" % retrlen + sf = StringIO(headers) + headers = mimetools.Message(sf) + return addinfourl(fp, headers, req.get_full_url()) + except ftplib.all_errors, msg: + raise IOError, ('ftp error', msg), sys.exc_info()[2] + + except socket.error, msg: + raise URLError(msg) def connect_ftp(self, user, passwd, host, port, dirs): fw = ftpwrapper(user, passwd, host, port, dirs)