Exemplo n.º 1
0
 def open_ftp(self, url):
     host, path = urllib.splithost(url)
     if not host: raise IOError, ('ftp error', 'no host given')
     host, port = urllib.splitport(host)
     user, host = urllib.splituser(host)
     # if user: user, passwd = splitpasswd(user)
     if user: passwd = getpass.getpass()
     else: passwd = None
     host = urllib.unquote(host)
     user = urllib.unquote(user or '')
     passwd = urllib.unquote(passwd or '')
     host = socket.gethostbyname(host)
     if not port:
         import ftplib
         port = ftplib.FTP_PORT
     else:
         port = int(port)
     path, attrs = urllib.splitattr(path)
     path = urllib.unquote(path)
     dirs = string.splitfields(path, '/')
     dirs, file = dirs[:-1], dirs[-1]
     if dirs and not dirs[0]: dirs = dirs[1:]
     key = (user, host, port, string.joinfields(dirs, '/'))
     # XXX thread unsafe!
     if len(self.ftpcache) > MAXFTPCACHE:
         # Prune the cache, rather arbitrarily
         for k in self.ftpcache.keys():
             if k != key:
                 v = self.ftpcache[k]
                 del self.ftpcache[k]
                 v.close()
     try:
         if not self.ftpcache.has_key(key):
             print 'Creating ftpwrapper: ',user,host,port,dirs
             self.ftpcache[key] = \
                 urllib.ftpwrapper(user, passwd, host, port, dirs)
         if not file: type = 'D'
         else: type = 'I'
         for attr in attrs:
             attr, value = urllib.splitvalue(attr)
             if string.lower(attr) == 'type' and \
                value in ('a', 'A', 'i', 'I', 'd', 'D'):
                 type = string.upper(value)
         (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
         if retrlen is not None and retrlen >= 0:
             import mimetools, StringIO
             headers = mimetools.Message(StringIO.StringIO(
                 'Content-Length: %d\n' % retrlen))
         else:
             headers = noheaders()
         return urllib.addinfourl(fp, headers, "ftp:" + url)
     except urllib.ftperrors(), msg:
         raise IOError, ('ftp error', msg), sys.exc_info()[2]
Exemplo n.º 2
0
 def open_ftp(self, url):
     host, path = urllib.splithost(url)
     if not host: raise IOError, ('ftp error', 'no host given')
     host, port = urllib.splitport(host)
     user, host = urllib.splituser(host)
     # if user: user, passwd = splitpasswd(user)
     if user: passwd = getpass.getpass()
     else: passwd = None
     host = urllib.unquote(host)
     user = urllib.unquote(user or '')
     passwd = urllib.unquote(passwd or '')
     host = socket.gethostbyname(host)
     if not port:
         import ftplib
         port = ftplib.FTP_PORT
     else:
         port = int(port)
     path, attrs = urllib.splitattr(path)
     path = urllib.unquote(path)
     dirs = string.splitfields(path, '/')
     dirs, file = dirs[:-1], dirs[-1]
     if dirs and not dirs[0]: dirs = dirs[1:]
     key = (user, host, port, string.joinfields(dirs, '/'))
     # XXX thread unsafe!
     if len(self.ftpcache) > MAXFTPCACHE:
         # Prune the cache, rather arbitrarily
         for k in self.ftpcache.keys():
             if k != key:
                 v = self.ftpcache[k]
                 del self.ftpcache[k]
                 v.close()
     try:
         if not self.ftpcache.has_key(key):
             print 'Creating ftpwrapper: ', user, host, port, dirs
             self.ftpcache[key] = \
                 urllib.ftpwrapper(user, passwd, host, port, dirs)
         if not file: type = 'D'
         else: type = 'I'
         for attr in attrs:
             attr, value = urllib.splitvalue(attr)
             if string.lower(attr) == 'type' and \
                value in ('a', 'A', 'i', 'I', 'd', 'D'):
                 type = string.upper(value)
         (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
         if retrlen is not None and retrlen >= 0:
             import mimetools, StringIO
             headers = mimetools.Message(
                 StringIO.StringIO('Content-Length: %d\n' % retrlen))
         else:
             headers = noheaders()
         return urllib.addinfourl(fp, headers, "ftp:" + url)
     except urllib.ftperrors(), msg:
         raise IOError, ('ftp error', msg), sys.exc_info()[2]
Exemplo n.º 3
0
 def open_ftp(self, url):
     """Use FTP protocol."""
     if not isinstance(url, str):
         raise IOError(('ftp error', 'proxy support for ftp protocol currently not implemented'))
     import mimetypes
     import mimetools
     try:
         from cStringIO import StringIO
     except ImportError:
         from StringIO import StringIO
     host, path = urllib.splithost(url)
     if not host:
         raise IOError(('ftp error', 'no host given'))
     host, port = urllib.splitport(host)
     user, host = urllib.splituser(host)
     if user:
         user, passwd = urllib.splitpasswd(user)
     else:
         passwd = None
     host = urllib.unquote(host)
     user = urllib.unquote(user or '')
     passwd = urllib.unquote(passwd or '')
     host = socket.gethostbyname(host)
     if not port:
         import ftplib  # noqa
         port = ftplib.FTP_PORT
     else:
         port = int(port)
     path, attrs = urllib.splitattr(path)
     path = urllib.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) > urllib.MAXFTPCACHE:
         # Prune the cache, rather arbitrarily
         for k in self.ftpcache.keys():
             if k != key:
                 v = self.ftpcache[k]
                 del self.ftpcache[k]
                 v.close()
     try:
         if not key in self.ftpcache:
             self.ftpcache[key] = \
                 Myftpwrapper(user, passwd, host, port, dirs)
         if not file:
             type = 'D'
         else:
             type = 'I'
         for attr in attrs:
             attr, value = urllib.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,
                                                     rest=os.environ.get("REST"))
         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 = mimetools.Message(StringIO(headers))
         return urllib.addinfourl(fp, headers, "ftp:" + url)
     except urllib.ftperrors() as msg:
         raise IOError(('ftp error', msg), sys.exc_info()[2])