parameters: (self, url)
docstring: Use FTP protocol.
docstring_summary: Use FTP protocol.
docstring_tokens: Use FTP protocol .
function: 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])
function_tokens: 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 ] )