示例#1
0
    def retrfile(self, file, type):
        self.endtransfer()
        if type in ('d', 'D'):
            cmd = 'TYPE A'
            isdir = 1
        else:
            cmd = 'TYPE ' + type
            isdir = 0
        try:
            self.ftp.voidcmd(cmd)
        except ftplib.all_errors:
            self.init()
            # TODO catch this error
            #try:
            self.ftp.voidcmd(cmd)
            #except: pass
        conn = None
        if file and not isdir:
            # Try to retrieve as a file
            try:
                cmd = 'RETR ' + self.dir + file
                conn = self.ftp.ntransfercmd(cmd)
            except ftplib.error_perm as reason:
                if str(reason)[:3] != '550':
                    raise IOError(('ftp error', reason), sys.exc_info()[2])

        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (urllib.addclosehook(conn[0].makefile('rb'),
                                    self.endtransfer), conn[1])
示例#2
0
 def retrfile(self, file, type):
     self.endtransfer()
     if type in ('d', 'D'): cmd = 'TYPE A'; isdir = 1
     else: cmd = 'TYPE ' + type; isdir = 0
     try:
         self.ftp.voidcmd(cmd)
     except ftplib.all_errors:
         self.init()
         # TODO catch this error
         #try:
         self.ftp.voidcmd(cmd)
         #except: pass
     conn = None
     if file and not isdir:
         # Try to retrieve as a file
         try:
             cmd = 'RETR ' + self.dir + file
             conn = self.ftp.ntransfercmd(cmd)
         except ftplib.error_perm as reason:
             if str(reason)[:3] != '550':
                 raise IOError(('ftp error', reason), sys.exc_info()[2])
                 
     self.busy = 1
     # Pass back both a suitably decorated object and a retrieval length
     return (urllib.addclosehook(conn[0].makefile('rb'),
                          self.endtransfer), conn[1])
示例#3
0
class ftpwrapper(urllib.ftpwrapper):
    """Class used by open_ftp() for caching open FTP connections.

    The code was taken from urllib.py.
    The only difference is that offsets are supported by this class
    using the REST-command.
    """
    def retrfile(self, file, type, rest=None):
        self.endtransfer()
        if type in ('d', 'D'):
            cmd = 'TYPE A'
            isdir = 1
        else:
            cmd = 'TYPE ' + type
            isdir = 0
        try:
            self.ftp.voidcmd(cmd)
        except ftplib.all_errors:
            self.init()
            self.ftp.voidcmd(cmd)
        conn = None
        if file and not isdir:
            # Try to retrieve as a file
            try:
                cmd = 'RETR ' + file
                # EDIT START
                conn = self.ftp.ntransfercmd(cmd, rest)
                # EDIT END
            except ftplib.error_perm, reason:
                if str(reason)[:3] != '550':
                    raise IOError, ('ftp error', reason), sys.exc_info()[2]
        if not conn:
            # Set transfer mode to ASCII!
            self.ftp.voidcmd('TYPE A')
            # Try a directory listing. Verify that directory exists.
            if file:
                pwd = self.ftp.pwd()
                try:
                    try:
                        self.ftp.cwd(file)
                    except ftplib.error_perm, reason:
                        raise IOError, ('ftp error', reason), sys.exc_info()[2]
                finally:
                    self.ftp.cwd(pwd)
                cmd = 'LIST ' + file
            else:
                cmd = 'LIST'
            conn = self.ftp.ntransfercmd(cmd)
        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (addclosehook(conn[0].makefile('rb'),
                             self.endtransfer), conn[1])
示例#4
0
 def retrfile(self, file, type, rest=None):
     self.endtransfer()
     if type in ('d', 'D'):
         cmd = 'TYPE A'
         isdir = 1
     else:
         cmd = 'TYPE ' + type
         isdir = 0
     try:
         self.ftp.voidcmd(cmd)
     except ftplib.all_errors:
         self.init()
         self.ftp.voidcmd(cmd)
     conn = None
     if file and not isdir:
         # Use nlst to see if the file exists at all
         try:
             self.ftp.nlst(file)
         except ftplib.error_perm as reason:
             raise IOError('ftp error', reason)
         # Restore the transfer mode!
         self.ftp.voidcmd(cmd)
         # Try to retrieve as a file
         try:
             cmd = 'RETR ' + file
             conn = self.ftp.ntransfercmd(cmd, rest)
         except ftplib.error_perm as reason:
             if str(reason).startswith('501'):
                 # workaround for REST not supported error
                 fp, retrlen = self.retrfile(file, type)
                 fp = RangeableFileObject(fp, (rest,''))
                 return (fp, retrlen)
             elif not str(reason).startswith('550'):
                 raise IOError('ftp error', reason)
     if not conn:
         # Set transfer mode to ASCII!
         self.ftp.voidcmd('TYPE A')
         # Try a directory listing
         if file:
             cmd = 'LIST ' + file
         else:
             cmd = 'LIST'
         conn = self.ftp.ntransfercmd(cmd)
     self.busy = 1
     # Pass back both a suitably decorated object and a retrieval length
     return (addclosehook(conn[0].makefile('rb'),
                         self.endtransfer), conn[1])
示例#5
0
文件: proxy.py 项目: bneijt/metalinks
class ftpwrapper(urllib.ftpwrapper):
    """Class used by open_ftp() for cache of open FTP connections."""
    def init(self):
        self.busy = 0
        self.ftp = FTP()
        self.ftp.connect(self.host, self.port, self.timeout)
        self.ftp.login(self.user, self.passwd)
        self.dir = "/" + "/".join(self.dirs) + "/"

    def retrfile(self, file, type):
        self.endtransfer()
        if type in ('d', 'D'):
            cmd = 'TYPE A'
            isdir = 1
        else:
            cmd = 'TYPE ' + type
            isdir = 0
        try:
            self.ftp.voidcmd(cmd)
        except ftplib.all_errors:
            self.init()
            # TODO catch this error
            #try:
            self.ftp.voidcmd(cmd)
            #except: pass
        conn = None
        if file and not isdir:
            # Try to retrieve as a file
            try:
                cmd = 'RETR ' + self.dir + file
                conn = self.ftp.ntransfercmd(cmd)
            except ftplib.error_perm, reason:
                if str(reason)[:3] != '550':
                    raise IOError, ('ftp error', reason), sys.exc_info()[2]

        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (urllib.addclosehook(conn[0].makefile('rb'),
                                    self.endtransfer), conn[1])
示例#6
0
 def retrfile(self, file, type, rest=None):
     import ftplib  # noqa
     self.endtransfer()
     if type in ('d', 'D'):
         cmd = 'TYPE A'
         isdir = 1
     else:
         cmd = 'TYPE ' + type
         isdir = 0
     try:
         self.ftp.voidcmd(cmd)
     except ftplib.all_errors:
         self.init()
         self.ftp.voidcmd(cmd)
     conn = None
     if file and not isdir:
         # Try to retrieve as a file
         try:
             cmd = 'RETR ' + file
             conn = self.ftp.ntransfercmd(cmd, rest=rest)
         except ftplib.error_perm as reason:
             if str(reason)[:3] != '550':
                 raise IOError(('ftp error', reason), sys.exc_info()[2])
     if not conn:
         # Set transfer mode to ASCII!
         self.ftp.voidcmd('TYPE A')
         # Try a directory listing
         if file:
             cmd = 'LIST ' + file
         else:
             cmd = 'LIST'
         conn = self.ftp.ntransfercmd(cmd)
     self.busy = 1
     # Pass back both a suitably decorated object and a retrieval length
     return (urllib.addclosehook(conn[0].makefile('rb'),
                                 self.endtransfer), conn[1])
示例#7
0
            self.init()
            self.ftp.voidcmd(cmd)
        conn = None
        if file and not isdir:
            try:
                self.ftp.nlst(file)
            except ftplib.error_perm, reason:
                raise IOError, (_('ftp error'), reason), sys.exc_info()[2]
            # Restore the transfer mode!
            self.ftp.voidcmd(cmd)
            try:
                cmd = 'RETR ' + file
                conn = self.ftp.ntransfercmd(cmd, rest)
            except ftplib.error_perm, reason:
                if str(reason)[:3] == '501':
                    # workaround for REST not suported error
                    fp, retrlen = self.retrfile(file, type)
                    fp = RangeableFileObject(fp, (rest,''))
                    return (fp, retrlen)
                elif str(reason)[:3] != '550':
                    raise IOError, (_('ftp error'), reason), sys.exc_info()[2]
        if not conn:
            self.ftp.voidcmd('TYPE A')
            if file: cmd = 'LIST ' + file
            else: cmd = 'LIST'
            conn = self.ftp.ntransfercmd(cmd)
        self.busy = 1
        return (urllib.addclosehook(conn[0].makefile('rb'),
                            self.endtransfer), conn[1])

示例#8
0
                    fp = RangeableFileObject(fp, (rest,''))
                    return (fp, retrlen)
                elif not str(reason).startswith('550'):
                    raise IOError, ('ftp error', reason), sys.exc_info()[2]
        if not conn:
            # Set transfer mode to ASCII!
            self.ftp.voidcmd('TYPE A')
            # Try a directory listing
            if file:
                cmd = 'LIST ' + file
            else:
                cmd = 'LIST'
            conn = self.ftp.ntransfercmd(cmd)
        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (addclosehook(conn[0].makefile('rb'),
                            self.endtransfer), conn[1])


####################################################################
# Range Tuple Functions
# XXX: These range tuple functions might go better in a class.

_rangere = None
def range_header_to_tuple(range_header):
    """Get a (firstbyte,lastbyte) tuple from a Range header value.

    Range headers have the form "bytes=<firstbyte>-<lastbyte>". This
    function pulls the firstbyte and lastbyte values and returns
    a (firstbyte,lastbyte) tuple. If lastbyte is not specified in
    the header value, it is returned as an empty string in the
    tuple.
示例#9
0
                    fp = RangeableFileObject(fp, (rest, ''))
                    return (fp, retrlen)
                elif not str(reason).startswith('550'):
                    raise IOError('ftp error', reason)
        if not conn:
            # Set transfer mode to ASCII!
            self.ftp.voidcmd('TYPE A')
            # Try a directory listing
            if file:
                cmd = 'LIST ' + file
            else:
                cmd = 'LIST'
            conn = self.ftp.ntransfercmd(cmd)
        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (addclosehook(conn[0].makefile('rb'), self.endtransfer),
                conn[1])


####################################################################
# Range Tuple Functions
# XXX: These range tuple functions might go better in a class.

_rangere = None


def range_header_to_tuple(range_header):
    """Get a (firstbyte,lastbyte) tuple from a Range header value.

    Range headers have the form "bytes=<firstbyte>-<lastbyte>". This
    function pulls the firstbyte and lastbyte values and returns
示例#10
0
                    fp = RangeableFileObject(fp, (rest, ""))
                    return (fp, retrlen)
                elif str(reason)[:3] != "550":
                    raise IOError, ("ftp error", reason), sys.exc_info()[2]
        if not conn:
            # Set transfer mode to ASCII!
            self.ftp.voidcmd("TYPE A")
            # Try a directory listing
            if file:
                cmd = "LIST " + file
            else:
                cmd = "LIST"
            conn = self.ftp.ntransfercmd(cmd)
        self.busy = 1
        # Pass back both a suitably decorated object and a retrieval length
        return (addclosehook(conn[0].makefile("rb"), self.endtransfer), conn[1])


####################################################################
# Range Tuple Functions
# XXX: These range tuple functions might go better in a class.

_rangere = None


def range_header_to_tuple(range_header):
    """Get a (firstbyte,lastbyte) tuple from a Range header value.
    
    Range headers have the form "bytes=<firstbyte>-<lastbyte>". This
    function pulls the firstbyte and lastbyte values and returns
    a (firstbyte,lastbyte) tuple. If lastbyte is not specified in