Python ftplib.error_perm() Examples
The following are 30
code examples of ftplib.error_perm().
These examples are extracted from open source projects.
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 Project: meddle Author: glmcdona File: urllib.py License: MIT License | 7 votes |
def retrfile(self, file, type): import ftplib 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) except ftplib.error_perm, reason: if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2]
Example #2
Source Project: ironpython2 Author: IronLanguages File: urllib.py License: Apache License 2.0 | 6 votes |
def retrfile(self, file, type): import ftplib 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, retrlen = self.ftp.ntransfercmd(cmd) except ftplib.error_perm, reason: if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2]
Example #3
Source Project: BinderFilter Author: dxwu File: urllib.py License: MIT License | 6 votes |
def retrfile(self, file, type): import ftplib 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, retrlen = self.ftp.ntransfercmd(cmd) except ftplib.error_perm, reason: if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2]
Example #4
Source Project: Computable Author: ktraunmueller File: urllib.py License: MIT License | 6 votes |
def retrfile(self, file, type): import ftplib 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) except ftplib.error_perm, reason: if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2]
Example #5
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_rnfr_rnto(self): # rename file tempname = os.path.basename(tempfile.mktemp(dir=HOME)) self.client.rename(self.tempfile, tempname) self.client.rename(tempname, self.tempfile) # rename dir tempname = os.path.basename(tempfile.mktemp(dir=HOME)) self.client.rename(self.tempdir, tempname) self.client.rename(tempname, self.tempdir) # rnfr/rnto over non-existing paths bogus = os.path.basename(tempfile.mktemp(dir=HOME)) self.assertRaises(ftplib.error_perm, self.client.rename, bogus, '/x') self.assertRaises( ftplib.error_perm, self.client.rename, self.tempfile, u('/')) # rnto sent without first specifying the source self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'rnto ' + self.tempfile) # make sure we can't rename root directory self.assertRaisesRegex(ftplib.error_perm, "Can't rename home directory", self.client.rename, '/', '/x')
Example #6
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_unforeseen_mdtm_event(self): # Emulate a case where the file last modification time is prior # to year 1900. This most likely will never happen unless # someone specifically force the last modification time of a # file in some way. # To do so we temporarily override os.path.getmtime so that it # returns a negative value referring to a year prior to 1900. # It causes time.localtime/gmtime to raise a ValueError exception # which is supposed to be handled by server. # On python 3 it seems that the trick of replacing the original # method with the lambda doesn't work. if not PY3: _getmtime = AbstractedFS.getmtime try: AbstractedFS.getmtime = lambda x, y: -9000000000 self.assertRaisesRegex( ftplib.error_perm, "550 Can't determine file's last modification time", self.client.sendcmd, 'mdtm ' + self.tempfile) # make sure client hasn't been disconnected self.client.sendcmd('noop') finally: AbstractedFS.getmtime = _getmtime
Example #7
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_stou_orphaned_file(self): # Check that no orphaned file gets left behind when STOU fails. # Even if STOU fails the file is first created and then erased. # Since we can't know the name of the file the best way that # we have to test this case is comparing the content of the # directory before and after STOU has been issued. # Assuming that TESTFN is supposed to be a "reserved" file # name we shouldn't get false positives. safe_remove(TESTFN) # login as a limited user in order to make STOU fail self.client.login('anonymous', '@nopasswd') before = os.listdir(HOME) self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'stou ' + TESTFN) after = os.listdir(HOME) if before != after: for file in after: self.assertFalse(file.startswith(TESTFN))
Example #8
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_epsv(self): # test wrong proto try: self.client.sendcmd('epsv ' + self.other_proto) except ftplib.error_perm as err: self.assertEqual(str(err)[0:3], "522") else: self.fail("Exception not raised") # proto > 2 self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'epsv 3') # test connection for cmd in ('EPSV', 'EPSV ' + self.proto): host, port = ftplib.parse229(self.client.sendcmd(cmd), self.client.sock.getpeername()) with contextlib.closing( socket.socket(self.client.af, socket.SOCK_STREAM)) as s: s.settimeout(TIMEOUT) s.connect((host, port)) self.client.sendcmd('abor')
Example #9
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_mlst(self): # utility function for extracting the line of interest def mlstline(cmd): return self.client.voidcmd(cmd).split('\n')[1] if self.utf8fs: self.assertTrue('type=dir' in mlstline('mlst ' + TESTFN_UNICODE)) self.assertTrue('/' + TESTFN_UNICODE in mlstline('mlst ' + TESTFN_UNICODE)) self.assertTrue('type=file' in mlstline('mlst ' + TESTFN_UNICODE_2)) self.assertTrue('/' + TESTFN_UNICODE_2 in mlstline('mlst ' + TESTFN_UNICODE_2)) else: self.assertRaises(ftplib.error_perm, mlstline, 'mlst ' + TESTFN_UNICODE) # --- file transfer
Example #10
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 6 votes |
def test_stor(self): if self.utf8fs: data = b'abcde12345' * 500 os.remove(TESTFN_UNICODE_2) dummy = BytesIO() dummy.write(data) dummy.seek(0) self.client.storbinary('stor ' + TESTFN_UNICODE_2, dummy) dummy_recv = BytesIO() self.client.retrbinary('retr ' + TESTFN_UNICODE_2, dummy_recv.write) dummy_recv.seek(0) self.assertEqual(dummy_recv.read(), data) else: dummy = BytesIO() self.assertRaises(ftplib.error_perm, self.client.storbinary, 'stor ' + TESTFN_UNICODE_2, dummy)
Example #11
Source Project: oss-ftp Author: aliyun File: test_functional_ssl.py License: MIT License | 6 votes |
def test_prot(self): self.client.login(secure=False) msg = "503 PROT not allowed on insecure control connection." self.assertRaisesWithMsg(ftplib.error_perm, msg, self.client.sendcmd, 'prot p') self.client.login(secure=True) # secured self.client.prot_p() sock = self.client.transfercmd('list') with contextlib.closing(sock): while 1: if not sock.recv(1024): self.client.voidresp() break self.assertTrue(isinstance(sock, ssl.SSLSocket)) # unsecured self.client.prot_c() sock = self.client.transfercmd('list') with contextlib.closing(sock): while 1: if not sock.recv(1024): self.client.voidresp() break self.assertFalse(isinstance(sock, ssl.SSLSocket))
Example #12
Source Project: oss-ftp Author: aliyun File: urllib.py License: MIT License | 6 votes |
def retrfile(self, file, type): import ftplib 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, retrlen = self.ftp.ntransfercmd(cmd) except ftplib.error_perm, reason: if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2]
Example #13
Source Project: deep-learning-note Author: wdxtub File: 1_ftp_client.py License: MIT License | 5 votes |
def main(): try: f = ftplib.FTP(HOST) except (socket.error, socket.gaierror) as e: print('ERROR: 无法连接 "{}"'.format(HOST)) return print('*** 已连接到 "{}"'.format(HOST)) try: f.login() except ftplib.error_perm: print('ERROR: 无法匿名登录') f.quit() return print('*** 已匿名身份登录') try: f.cwd(DIRN) except ftplib.error_perm: print('ERROR: 无法跳转到 "{}" 目录'.format(DIRN)) f.quit() return print('*** 跳转到 "{}" 目录'.format(DIRN)) try: f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write) except ftplib.error_perm: print('ERROR: 无法读取文件 "{}"'.format(FILE)) os.unlink(FILE) else: print('*** 已下载 "{}" 到当前目录'.format(FILE)) f.quit()
Example #14
Source Project: ibllib Author: int-brain-lab File: onelight.py License: MIT License | 5 votes |
def upload(self, root_dir, base_dir=None): root_dir = Path(root_dir) base_dir = base_dir or root_dir # Write the .one_root file iteratively. if self._writer is None: self._fr = open(root_dir / '.one_root', 'w') self._writer = csv.writer(self._fr, delimiter='\t') for name in sorted(os.listdir(root_dir)): path = Path(op.join(root_dir, name)) rel_path = path.relative_to(base_dir) if op.isfile(path) and is_file_in_session_dir(path): logger.debug("Upload %s.", path) self._writer.writerow([rel_path]) with open(path, 'rb') as f: self._ftp.storbinary('STOR ' + name, f) elif op.isdir(path): try: logger.debug("Create FTP dir %s.", name) self._ftp.mkd(name) except error_perm as e: if not e.args[0].startswith('550'): raise self._ftp.cwd(name) self.upload(path, base_dir=base_dir) self._ftp.cwd("..") # End: close the file and the FTP connection. if base_dir == root_dir: with open(root_dir / '.one_root', 'rb') as f: self._ftp.storbinary('STOR .one_root', f) self._fr.close() self._ftp.quit() # ------------------------------------------------------------------------------------------------- # HTTP ONE # -------------------------------------------------------------------------------------------------
Example #15
Source Project: magpy Author: geomagpy File: transfer.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def ftpdirlist (**kwargs): """ Getting directory listing of ftp server """ plog = PyMagLog() ftppath = kwargs.get('ftppath') myproxy = kwargs.get('myproxy') port = kwargs.get('port') login = kwargs.get('login') passwd = kwargs.get('passwd') try: site = ftplib.FTP() site.connect(myproxy, port) site.set_debuglevel(1) msg = site.login(login,passwd) site.cwd(ftppath) try: files=site.nlst() except ftplib.error_perm as resp: if str(resp) == "550 No files found": plog.addwarn("no files in this directory") else: raise pass site.quit() except: plog.addwarn("FTP check failed") return return files # #################### # 2. ftp: remove files # ####################
Example #16
Source Project: filesystem_spec Author: intake File: ftp.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def ls(self, path, detail=True, **kwargs): path = self._strip_protocol(path) out = [] if path not in self.dircache: try: try: out = [ (fn, details) for (fn, details) in self.ftp.mlsd(path) if fn not in [".", ".."] and details["type"] not in ["pdir", "cdir"] ] except error_perm: out = _mlsd2(self.ftp, path) # Not platform independent for fn, details in out: if path == "/": path = "" # just for forming the names, below details["name"] = "/".join([path, fn.lstrip("/")]) if details["type"] == "file": details["size"] = int(details["size"]) else: details["size"] = 0 if details["type"] == "dir": details["type"] = "directory" self.dircache[path] = out except Error: try: info = self.info(path) if info["type"] == "file": out = [(path, info)] except (Error, IndexError): raise FileNotFoundError files = self.dircache.get(path, out) if not detail: return sorted([fn for fn, details in files]) return [details for fn, details in files]
Example #17
Source Project: apt2 Author: tatanus File: scan_anonftp.py License: MIT License | 5 votes |
def testTarget(self, host, port): # verify we have not tested this host before if not self.seentarget(host + str(port)): self.addseentarget(host + str(port)) self.display.verbose(self.shortName + " - Connecting to " + host) # start packet capture cap = self.pktCap(filter="tcp and port " + str(port) + " and host " + host, packetcount=10, timeout=10, srcip=self.config['lhost'], dstip=host) # connect to the target host ftp = FTP() try: ftp.connect(host, int(port)) outfile = self.config["proofsDir"] + self.shortName + "_PCAP_Port" + str( port) + "_" + host + "_" + Utils.getRandStr(10) try: # attempt to login as anonymous result = ftp.login("anonymous", "anon@mo.us") if ("Login successful" in result): # fire a new trigger self.fire("anonymousFtp") self.addVuln(host, "anonymousFTP", {"port": str(port), "output": outfile.replace("/", "%2F")}) self.display.error("VULN [AnonymousFTP] Found on [%s]" % host) else: self.display.verbose("Could not login as anonymous to FTP at " + host) except error_perm as e: self.display.verbose("Could not login as anonymous to FTP at " + host) # close the connection ftp.close() # retrieve pcap results Utils.writeFile(self.getPktCap(cap), outfile) except EOFError as e: self.display.verbose("Could not find FTP server located at " + host + " Port " + str(port)) except socket.error as e: self.display.verbose("Could not find FTP server located at " + host + " Port " + str(port))
Example #18
Source Project: ironpython2 Author: IronLanguages File: test_ftplib.py License: Apache License 2.0 | 5 votes |
def test_exceptions(self): self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r\n0') self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\n0') self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r0') self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999')
Example #19
Source Project: ironpython2 Author: IronLanguages File: test_ftplib.py License: Apache License 2.0 | 5 votes |
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 #20
Source Project: heralding Author: johnnykv File: test_ftp.py License: GNU General Public License v3.0 | 5 votes |
def test_login(self): """Testing different login combinations""" def ftp_login(): ftp_client = FTP() ftp_client.connect('127.0.0.1', 8888, 1) # expect perm exception try: ftp_client.login('james', 'bond') _ = ftp_client.getresp() # NOQA except ftplib.error_perm: ftp_client.quit() options = { 'enabled': 'True', 'port': 0, 'banner': 'Test Banner', 'users': { 'test': 'test' }, 'protocol_specific_data': { 'max_attempts': 3, 'banner': 'test banner', 'syst_type': 'Test Type' } } ftp_capability = ftp.ftp(options, self.loop) server_coro = asyncio.start_server( ftp_capability.handle_session, '0.0.0.0', 8888, loop=self.loop) self.server = self.loop.run_until_complete(server_coro) ftp_task = self.loop.run_in_executor(None, ftp_login) self.loop.run_until_complete(ftp_task)
Example #21
Source Project: BinderFilter Author: dxwu File: test_ftplib.py License: MIT License | 5 votes |
def test_exceptions(self): self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999')
Example #22
Source Project: BinderFilter Author: dxwu File: test_ftplib.py License: MIT License | 5 votes |
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 #23
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def assert_auth_failed(self, user, passwd): self.assertRaisesRegex(ftplib.error_perm, '530 Authentication failed', self.client.login, user, passwd)
Example #24
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_wrong_cmds_order(self): self.assertRaisesRegex(ftplib.error_perm, '503 Login with USER first', self.client.sendcmd, 'pass ' + PASSWD) self.client.login(user=USER, passwd=PASSWD) self.assertRaisesRegex(ftplib.error_perm, "503 User already authenticated.", self.client.sendcmd, 'pass ' + PASSWD)
Example #25
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_rein(self): self.client.login(user=USER, passwd=PASSWD) self.client.sendcmd('rein') # user not authenticated, error response expected self.assertRaisesRegex(ftplib.error_perm, '530 Log in with USER and PASS first', self.client.sendcmd, 'pwd') # by logging-in again we should be able to execute a # file-system command self.client.login(user=USER, passwd=PASSWD) self.client.sendcmd('pwd')
Example #26
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_rein_during_transfer(self): # Test REIN while already authenticated and a transfer is # in progress. self.client.login(user=USER, passwd=PASSWD) data = b'abcde12345' * 1000000 self.file.write(data) self.file.close() conn = self.client.transfercmd('retr ' + TESTFN) self.addCleanup(conn.close) rein_sent = False bytes_recv = 0 while 1: chunk = conn.recv(BUFSIZE) if not chunk: break bytes_recv += len(chunk) self.dummyfile.write(chunk) if bytes_recv > INTERRUPTED_TRANSF_SIZE and not rein_sent: rein_sent = True # flush account, error response expected self.client.sendcmd('rein') self.assertRaisesRegex(ftplib.error_perm, '530 Log in with USER and PASS first', self.client.dir) # a 226 response is expected once tranfer finishes self.assertEqual(self.client.voidresp()[:3], '226') # account is still flushed, error response is still expected self.assertRaisesRegex(ftplib.error_perm, '530 Log in with USER and PASS first', self.client.sendcmd, 'size ' + TESTFN) # by logging-in again we should be able to execute a # filesystem command self.client.login(user=USER, passwd=PASSWD) self.client.sendcmd('pwd') self.dummyfile.seek(0) datafile = self.dummyfile.read() self.assertEqual(len(data), len(datafile)) self.assertEqual(hash(data), hash(datafile))
Example #27
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_user(self): # Test USER while already authenticated and no transfer # is in progress. self.client.login(user=USER, passwd=PASSWD) self.client.sendcmd('user ' + USER) # authentication flushed self.assertRaisesRegex(ftplib.error_perm, '530 Log in with USER and PASS first', self.client.sendcmd, 'pwd') self.client.sendcmd('pass ' + PASSWD) self.client.sendcmd('pwd')
Example #28
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_type(self): self.client.sendcmd('type a') self.client.sendcmd('type i') self.client.sendcmd('type l7') self.client.sendcmd('type l8') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'type ?!?')
Example #29
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_stru(self): self.client.sendcmd('stru f') self.client.sendcmd('stru F') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'stru p') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'stru r') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'stru ?!?')
Example #30
Source Project: oss-ftp Author: aliyun File: test_functional.py License: MIT License | 5 votes |
def test_mode(self): self.client.sendcmd('mode s') self.client.sendcmd('mode S') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'mode b') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'mode c') self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'mode ?!?')