一直都觉得FTP现在用的少了,所以就一直没有做过这类的笔记,记得只是很早之前用PHP操作FTP的记录,这次写了下顺便记录下来了,方便以后直接使用,主要代码如下:
#!/usr/bin/python3 #coding:utf-8 from ftplib import FTP import os import logging logging.basicConfig(filename='app.log',level=logging.INFO,format='%(asctime)s %(message)s',datefmt='%Y-%m-%d %H:%M:%S') ftp_host = "192.168.0.7" ftp_user = "test" ftp_pass = "12345678" ftp_port = 21 ftp_dir = "/" ftp = FTP() def connFtpServer(): try: ftp.connect(ftp_host) ftp.login(ftp_user, ftp_pass) except: logging.error("无法连接FTP服务器或账户密码错误 !") def uploadLocalFile(filename): connFtpServer() try: ftp.cwd(ftp_dir) file = open(filename, 'rb') #pasv 主动模式 ftp.set_pasv(0) ftp.storbinary('STOR %s' % os.path.basename(filename),file) logging.info("%s 文件上传上成功!" % filename) file.close() ftp.close() except Exception as e: logging.error("上传失败, %s " % e) def downloadRemotefile(remotefilename): connFtpServer() try: #pasv 主动模式 ftp.set_pasv(0) filename = "local/" + os.path.basename(remotefilename) file = open(filename, 'wb') ftp.retrbinary('RETR '+remotefilename,file.write,1024) logging.info("%s 文件下载成功!" % os.path.basename(remotefilename)) file.close() ftp.close() except Exception as e: logging.error("%s 文件下载失败, %s" % (os.path.basename(remotefilename), e)) def listRemoteFile(): connFtpServer() filelist = False try: ftp.cwd(ftp_dir) #该返回文件列表包含目录 filelist = ftp.nlst() except Exception as e: logging.error("无法获取FTP文件列表 %s" % e) return filelist if __name__ == "__main__": #uploadLocalFile(r"C:\Users\yang.su\Desktop\local\app.log") downloadRemotefile("f.png") #print(listRemoteFile())
个人还是喜欢函数式编程,下面也列一个面向对象的写法吧
#!/usr/bin/python3 #coding:utf-8 from ftplib import FTP from log import info_output,error_output,warning_output class FtpServer: def __init__(self, ftp_host, ftp_user, ftp_pass, ftp_port=21): self.ftp = FTP() self.ftp_host = ftp_host self.ftp_user = ftp_user self.ftp_pass = ftp_pass self.ftp_port = ftp_port self.connFtpserver() def connFtpserver(self): try: self.ftp.connect(self.ftp_host, self.ftp_port) self.ftp.login(self.ftp_user, self.ftp_pass) #主动模式 self.ftp.set_pasv(0) except: error_output("FTP登陆失败!") def downloadRemoteFile(self, local_dir, remote_dir, remote_filename): flag = False try: self.ftp.cwd(remote_dir) file = open(local_dir+remote_filename, "wb") self.ftp.retrbinary('RETR '+remote_filename, file.write, 1024) info_output("%s 文件下载成功!" % remote_filename) file.close() flag = True except Exception as e: error_output("%s 文件下载失败, %s" % (remote_filename, e)) # finally: # self.ftp.close() return flag def uploadLocalFile(self, local_dir, local_filename, remote_dir): flag = False try: self.ftp.cwd(remote_dir) file = open(local_dir+local_filename, "rb") self.ftp.storbinary('STOR %s' % local_filename, file) info_output("%s 文件上传上成功!" % local_filename) file.close() flag = True except Exception as e: error_output("上传失败, %s " % e) finally: self.ftp.close() return flag def listRemoteFile(self, remote_dir): file_list = False try: self.ftp.cwd(remote_dir) file_list = self.ftp.nlst() except Exception as e: error_output("无法获取FTP文件列表 %s" % e) # finally: # self.ftp.close() return file_list def RemoteFileSize(self, remote_dir, filename): file_size = False try: self.ftp.cwd(remote_dir) file_size = self.ftp.size(filename) except: error_output("获取 %s 文件大小失败!" % filename) # finally: # self.ftp.close() return file_size def closeFtpserver(self): self.ftp.close()
虽然以前用的PHP较多,但是觉得python上手还是更方便些,然后以前的php操作FTP类可以查看我之前的笔记:https://sulao.cn/post/461.html