(self, url)
Use FTP protocol.
Use FTP protocol.
Use FTP protocol .
def open_ftp(self, url):
        """Use FTP protocol."""
        if not isinstance(url, str):
            raise URLError('ftp error: proxy support for ftp protocol currently not implemented')
        import mimetypes
        host, path = _splithost(url)
        if not host: raise URLError('ftp error: no host given')
        host, port = _splitport(host)
        user, host = _splituser(host)
        if user: user, passwd = _splitpasswd(user)
        else: passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')
        host = socket.gethostbyname(host)
        if not port:
            import ftplib
            port = ftplib.FTP_PORT
        else:
            port = int(port)
        path, attrs = _splitattr(path)
        path = unquote(path)
        dirs = path.split('/')
        dirs, file = dirs[:-1], dirs[-1]
        if dirs and not dirs[0]: dirs = dirs[1:]
        if dirs and not dirs[0]: dirs[0] = '/'
        key = user, host, port, '/'.join(dirs)
        # XXX thread unsafe!
        if len(self.ftpcache) > MAXFTPCACHE:
            # Prune the cache, rather arbitrarily
            for k in list(self.ftpcache):
                if k != key:
                    v = self.ftpcache[k]
                    del self.ftpcache[k]
                    v.close()
        try:
            if key not in self.ftpcache:
                self.ftpcache[key] = \
                    ftpwrapper(user, passwd, host, port, dirs)
            if not file: type = 'D'
            else: type = 'I'
            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) = self.ftpcache[key].retrfile(file, type)
            mtype = mimetypes.guess_type("ftp:" + url)[0]
            headers = ""
            if mtype:
                headers += "Content-Type: %s\n" % mtype
            if retrlen is not None and retrlen >= 0:
                headers += "Content-Length: %d\n" % retrlen
            headers = email.message_from_string(headers)
            return addinfourl(fp, headers, "ftp:" + url)
        except ftperrors() as exp:
            raise URLError('ftp error %r' % exp).with_traceback(sys.exc_info()[2])
def open_ftp ( self , url ) : if not isinstance ( url , str ) : raise URLError ( 'ftp error: proxy support for ftp protocol currently not implemented' ) import mimetypes host , path = _splithost ( url ) if not host : raise URLError ( 'ftp error: no host given' ) host , port = _splitport ( host ) user , host = _splituser ( host ) if user : user , passwd = _splitpasswd ( user ) else : passwd = None host = unquote ( host ) user = unquote ( user or '' ) passwd = unquote ( passwd or '' ) host = socket . gethostbyname ( host ) if not port : import ftplib port = ftplib . FTP_PORT else : port = int ( port ) path , attrs = _splitattr ( path ) path = unquote ( path ) dirs = path . split ( '/' ) dirs , file = dirs [ : - 1 ] , dirs [ - 1 ] if dirs and not dirs [ 0 ] : dirs = dirs [ 1 : ] if dirs and not dirs [ 0 ] : dirs [ 0 ] = '/' key = user , host , port , '/' . join ( dirs ) # XXX thread unsafe! if len ( self . ftpcache ) > MAXFTPCACHE : # Prune the cache, rather arbitrarily for k in list ( self . ftpcache ) : if k != key : v = self . ftpcache [ k ] del self . ftpcache [ k ] v . close ( ) try : if key not in self . ftpcache : self . ftpcache [ key ] = ftpwrapper ( user , passwd , host , port , dirs ) if not file : type = 'D' else : type = 'I' 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 ) = self . ftpcache [ key ] . retrfile ( file , type ) mtype = mimetypes . guess_type ( "ftp:" + url ) [ 0 ] headers = "" if mtype : headers += "Content-Type: %s\n" % mtype if retrlen is not None and retrlen >= 0 : headers += "Content-Length: %d\n" % retrlen headers = email . message_from_string ( headers ) return addinfourl ( fp , headers , "ftp:" + url ) except ftperrors ( ) as exp : raise URLError ( 'ftp error %r' % exp ) . with_traceback ( sys . exc_info ( ) [ 2 ] )