Python 操作FTP

 import sys, os, ftplib, socket

 CONST_HOST = "FTP服务器地址"
CONST_USERNAME = "FTP用户名"
CONST_PWD = "FTP密码" CONST_BUFFER_SIZE = class FtpHelper(object):
ftp = None def __init__(self):
self.ftp = self.connect() def connect(self):
try:
ftp = ftplib.FTP(CONST_HOST)
ftp.login(CONST_USERNAME, CONST_PWD)
return ftp
except socket.error, socket.gaierror:
print("FTP is unavailable,please check the host,username and password!")
sys.exit() def disconnect(self):
self.ftp.quit() def upload(self, ftppath, filepath):
f = open(filepath, "rb")
self.ftp.cwd(ftppath)
file_name = os.path.split(filepath)[-]
try:
self.ftp.storbinary('STOR %s'%file_name, f, CONST_BUFFER_SIZE)
except ftplib.error_perm:
return False
return True def download(self, ftppath, filename):
f = open(filename, "wb").write
self.ftp.cwd(ftppath)
try:
self.ftp.retrbinary("RETR %s"%filename, f, CONST_BUFFER_SIZE)
except ftplib.error_perm:
return False
return True def list(self):
self.ftp.dir() def find(self, ftppath, filename):
self.ftp.cwd(ftppath)
ftp_f_list = self.ftp.nlst()
if filename in ftp_f_list:
return True
else:
return False if __name__ == "__main__":
fh = FtpHelper()
fh.list()
上一篇:快速傅里叶变换FFT / NTT


下一篇:ViewBag是如何实现的