本文整理汇总了Python中ftplib.error_perm方法的典型用法代码示例。如果您正苦于以下问题:Python ftplib.error_perm方法的具体用法?Python ftplib.error_perm怎么用?Python ftplib.error_perm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ftplib
的用法示例。
在下文中一共展示了ftplib.error_perm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrfile
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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]
示例2: retrfile
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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]
示例3: test_rnfr_rnto
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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')
示例4: test_unforeseen_mdtm_event
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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
示例5: test_stou_orphaned_file
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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))
示例6: test_epsv
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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')
示例7: test_mlst
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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
示例8: test_stor
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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)
示例9: test_prot
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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))
示例10: main
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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()
示例11: upload
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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
# -------------------------------------------------------------------------------------------------
示例12: ftpdirlist
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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
# ####################
示例13: ls
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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]
示例14: testTarget
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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))
示例15: test_exceptions
# 需要导入模块: import ftplib [as 别名]
# 或者: from ftplib import error_perm [as 别名]
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')