Python ftplib.Error() Examples

The following are 30 code examples of ftplib.Error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ftplib , or try the search function .
Example #1
Source File: test_functional.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_stor(self):
        try:
            data = b'abcde12345' * 100000
            self.dummy_sendfile.write(data)
            self.dummy_sendfile.seek(0)
            self.client.storbinary('stor ' + TESTFN, self.dummy_sendfile)
            self.client.retrbinary('retr ' + TESTFN, self.dummy_recvfile.write)
            self.dummy_recvfile.seek(0)
            datafile = self.dummy_recvfile.read()
            self.assertEqual(len(data), len(datafile))
            self.assertEqual(hash(data), hash(datafile))
        finally:
            # We do not use os.remove() because file could still be
            # locked by ftpd thread.  If DELE through FTP fails try
            # os.remove() as last resort.
            if os.path.exists(TESTFN):
                try:
                    self.client.delete(TESTFN)
                except (ftplib.Error, EOFError, socket.error):
                    safe_remove(TESTFN) 
Example #2
Source File: test_functional.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_stor(self):
        try:
            data = b'abcde12345' * 100000
            self.dummy_sendfile.write(data)
            self.dummy_sendfile.seek(0)
            self.client.storbinary('stor ' + TESTFN, self.dummy_sendfile)
            self.client.retrbinary('retr ' + TESTFN, self.dummy_recvfile.write)
            self.dummy_recvfile.seek(0)
            datafile = self.dummy_recvfile.read()
            self.assertEqual(len(data), len(datafile))
            self.assertEqual(hash(data), hash(datafile))
        finally:
            # We do not use os.remove() because file could still be
            # locked by ftpd thread.  If DELE through FTP fails try
            # os.remove() as last resort.
            if os.path.exists(TESTFN):
                try:
                    self.client.delete(TESTFN)
                except (ftplib.Error, EOFError, socket.error):
                    safe_remove(TESTFN) 
Example #3
Source File: test_ftplib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_storlines_too_long(self):
        f = io.BytesIO(b'x' * self.client.maxline * 2)
        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) 
Example #4
Source File: test_ftplib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_retrlines_too_long(self):
        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
        received = []
        self.assertRaises(ftplib.Error,
                          self.client.retrlines, 'retr', received.append) 
Example #5
Source File: test_ftplib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, OSError,
                      EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #6
Source File: test_ftplib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_line_too_long(self):
        self.assertRaises(ftplib.Error, self.client.sendcmd,
                          'x' * self.client.maxline * 2) 
Example #7
Source File: test_ftplib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_retrlines_too_long(self):
        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
        received = []
        self.assertRaises(ftplib.Error,
                          self.client.retrlines, 'retr', received.append) 
Example #8
Source File: test_ftplib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_storlines_too_long(self):
        f = io.BytesIO(b'x' * self.client.maxline * 2)
        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) 
Example #9
Source File: test_ftplib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, IOError, EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #10
Source File: test_ftplib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_too_long(self):
        self.assertRaises(ftplib.Error, self.client.sendcmd,
                          'x' * self.client.maxline * 2) 
Example #11
Source File: test_ftplib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_retrlines_too_long(self):
        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
        received = []
        self.assertRaises(ftplib.Error,
                          self.client.retrlines, 'retr', received.append) 
Example #12
Source File: test_ftplib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_storlines_too_long(self):
        f = StringIO.StringIO('x' * self.client.maxline * 2)
        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) 
Example #13
Source File: test_ftplib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, OSError,
                      EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #14
Source File: test_ftplib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_line_too_long(self):
        self.assertRaises(ftplib.Error, self.client.sendcmd,
                          'x' * self.client.maxline * 2) 
Example #15
Source File: test_ftplib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_retrlines_too_long(self):
        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
        received = []
        self.assertRaises(ftplib.Error,
                          self.client.retrlines, 'retr', received.append) 
Example #16
Source File: test_ftplib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_storlines_too_long(self):
        f = io.BytesIO(b'x' * self.client.maxline * 2)
        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) 
Example #17
Source File: patator_ext.py    From project-black with GNU General Public License v2.0 5 votes vote down vote up
def execute(self, host, port='21', tls='0', user=None, password=None, timeout='10', persistent='1'):

    try:
      with Timing() as timing:
        fp, resp = self.bind(host, port, tls, timeout=timeout)

      if user is not None or password is not None:
        with Timing() as timing:

          if user is not None:
            resp = fp.sendcmd('USER ' + user)

          if password is not None:
            resp = fp.sendcmd('PASS ' + password)

      logger.debug('No error: %r' % resp)
      self.reset()

    except FTP_Error as e:
      logger.debug('FTP_Error: %s' % e)
      resp = str(e)

    if persistent == '0':
      self.reset()

    code, mesg = resp.split(' ', 1)
    return self.Response(code, mesg, timing)

# }}}

# SSH {{{ 
Example #18
Source File: test_ftplib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, IOError, EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #19
Source File: test_functional.py    From pyftpdlib with MIT License 5 votes vote down vote up
def test_stou(self):
        data = b'abcde12345' * 100000
        self.dummy_sendfile.write(data)
        self.dummy_sendfile.seek(0)

        self.client.voidcmd('TYPE I')
        # filename comes in as "1xx FILE: <filename>"
        filename = self.client.sendcmd('stou').split('FILE: ')[1]
        try:
            with contextlib.closing(self.client.makeport()) as sock:
                conn, sockaddr = sock.accept()
                with contextlib.closing(conn):
                    conn.settimeout(TIMEOUT)
                    if hasattr(self.client_class, 'ssl_version'):
                        conn = ssl.wrap_socket(conn)
                    while True:
                        buf = self.dummy_sendfile.read(8192)
                        if not buf:
                            break
                        conn.sendall(buf)
            # transfer finished, a 226 response is expected
            self.assertEqual('226', self.client.voidresp()[:3])
            self.client.retrbinary('retr ' + filename,
                                   self.dummy_recvfile.write)
            self.dummy_recvfile.seek(0)
            datafile = self.dummy_recvfile.read()
            self.assertEqual(len(data), len(datafile))
            self.assertEqual(hash(data), hash(datafile))
        finally:
            # We do not use os.remove() because file could still be
            # locked by ftpd thread.  If DELE through FTP fails try
            # os.remove() as last resort.
            if os.path.exists(filename):
                try:
                    self.client.delete(filename)
                except (ftplib.Error, EOFError, socket.error):
                    safe_rmpath(filename) 
Example #20
Source File: test_functional.py    From pyftpdlib with MIT License 5 votes vote down vote up
def cmdresp(self, cmd):
        """Send a command and return response, also if the command failed."""
        try:
            return self.client.sendcmd(cmd)
        except ftplib.Error as err:
            return str(err) 
Example #21
Source File: esa_cci_ftp.py    From cate with MIT License 5 votes vote down vote up
def _start(self) -> DownloadStatus:
        local_dir = os.path.join(self._local_dir, self._path)
        os.makedirs(local_dir, exist_ok=True)
        local_file = os.path.join(local_dir, self._filename)
        if os.path.exists(local_file):
            local_size = os.path.getsize(local_file)
            # TODO (forman, 20160619): use 'modify' from file_info, to update outdated local files
            if local_size > 0 and local_size == self._file_size:
                self._monitor.progress(work=self._file_size, msg='local file is up-to-date')
                return DownloadStatus.SKIPPED
            else:
                # remove the old outdated file
                os.remove(local_file)
        rest = None
        filename_incomplete = self._filename + '.incomplete'
        local_file_incomplete = os.path.join(local_dir, filename_incomplete)
        if os.path.exists(local_file_incomplete):
            # TODO (forman, 20160619): reuse what has already been downloaded, then set variable 'rest' accordingly.
            # Brute force approach here: delete what has already been downloaded
            os.remove(local_file_incomplete)
        error_msg = None
        with open(local_file_incomplete, 'wb') as fp:
            self._fp = fp
            try:
                self._ftp.retrbinary('RETR ' + self._filename, self.on_new_block, blocksize=self._block_size, rest=rest)
            except KeyboardInterrupt:
                error_msg = 'download cancelled'
            except ftplib.Error as ftp_err:
                error_msg = 'download error: ' + str(ftp_err)
        # sys.stdout.write('\n')
        if error_msg is None:
            os.rename(local_file_incomplete, local_file)
        else:
            self._monitor.progress(msg=error_msg)
            os.remove(local_file_incomplete)
        return DownloadStatus.SUCCESS if error_msg is None else DownloadStatus.FAILURE 
Example #22
Source File: test_ftplib.py    From android_universal with MIT License 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, OSError,
                      EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #23
Source File: test_ftplib.py    From android_universal with MIT License 5 votes vote down vote up
def test_line_too_long(self):
        self.assertRaises(ftplib.Error, self.client.sendcmd,
                          'x' * self.client.maxline * 2) 
Example #24
Source File: test_ftplib.py    From android_universal with MIT License 5 votes vote down vote up
def test_retrlines_too_long(self):
        self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
        received = []
        self.assertRaises(ftplib.Error,
                          self.client.retrlines, 'retr', received.append) 
Example #25
Source File: test_ftplib.py    From android_universal with MIT License 5 votes vote down vote up
def test_storlines_too_long(self):
        f = io.BytesIO(b'x' * self.client.maxline * 2)
        self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) 
Example #26
Source File: test_ftplib.py    From android_universal with MIT License 5 votes vote down vote up
def test__all__(self):
        blacklist = {'MSG_OOB', 'FTP_PORT', 'MAXLINE', 'CRLF', 'B_CRLF',
                     'Error', 'parse150', 'parse227', 'parse229', 'parse257',
                     'print_line', 'ftpcp', 'test'}
        support.check__all__(self, ftplib, blacklist=blacklist) 
Example #27
Source File: test_ftplib.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_all_errors(self):
        exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
                      ftplib.error_proto, ftplib.Error, IOError, EOFError)
        for x in exceptions:
            try:
                raise x('exception not included in all_errors set')
            except ftplib.all_errors:
                pass 
Example #28
Source File: ftpfs.py    From pyfilesystem2 with MIT License 5 votes vote down vote up
def _parse_ftp_error(error):
    # type: (ftplib.Error) -> Tuple[Text, Text]
    """Extract code and message from ftp error."""
    code, _, message = text_type(error).partition(" ")
    return code, message 
Example #29
Source File: test_functional.py    From oss-ftp with MIT License 5 votes vote down vote up
def cmdresp(self, cmd):
        """Send a command and return response, also if the command failed."""
        try:
            return self.client.sendcmd(cmd)
        except ftplib.Error as err:
            return str(err) 
Example #30
Source File: ftp.py    From filesystem_spec with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _fetch_range(self, start, end):
        """Get bytes between given byte limits

        Implemented by raising an exception in the fetch callback when the
        number of bytes received reaches the requested amount.

        Will fail if the server does not respect the REST command on
        retrieve requests.
        """
        out = []
        total = [0]

        def callback(x):
            total[0] += len(x)
            if total[0] > end - start:
                out.append(x[: (end - start) - total[0]])
                if end < self.size:
                    raise TransferDone
            else:
                out.append(x)

            if total[0] == end - start and end < self.size:
                raise TransferDone

        try:
            self.fs.ftp.retrbinary(
                "RETR %s" % self.path,
                blocksize=self.blocksize,
                rest=start,
                callback=callback,
            )
        except TransferDone:
            try:
                # stop transfer, we got enough bytes for this block
                self.fs.ftp.abort()
                self.fs.ftp.getmultiline()
            except Error:
                self.fs.ftp._connect()

        return b"".join(out)