python 搭建FTP服务上传下载文件
2021/7/16 11:35:02
本文主要是介绍python 搭建FTP服务上传下载文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python 搭建FTP服务上传下载文件
-
server.py
#!/usr/bin/python3 # -*- coding:utf-8 -*- # @Author : Charlie Zhang # @Email : charlie.zhang@wiwide.com # @Time : 2021/7/15 16:20 # @Version : 1.0 # @File : server.py # @Software : PyCharm from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers import FTPServer from config import FTP_USER, FTP_PASSWORD, FTP_DIR, FTP_PASSIVE_PORTS # 实例化DummyAuthorizer来创建ftp用户 authorizer = DummyAuthorizer() # 参数:用户名,密码,目录,权限 authorizer.add_user(FTP_USER, FTP_PASSWORD, FTP_DIR, perm='elradfmwMT') # 匿名登录 # authorizer.add_anonymous('/home/nobody') handler = FTPHandler handler.authorizer = authorizer # Define a customized banner (string returned when client connects) handler.banner = "pyftpdlib based ftpd ready." handler.passive_ports = FTP_PASSIVE_PORTS # 参数:IP,端口,handler server = FTPServer(('127.0.0.1', 21), handler) # set a limit for connections server.max_cons = 256 server.max_cons_per_ip = 5 server.serve_forever()
-
client.py
#!/usr/bin/python3 # -*- coding:utf-8 -*- # @Author : Charlie Zhang # @Email : charlie.zhang@wiwide.com # @Time : 2021/7/15 16:23 # @Version : 1.0 # @File : client.py # @Software : PyCharm import ftplib import os from config import FTP_USER, FTP_PASSWORD, LOCAL_DIR class Ftp(ftplib.FTP): def __init__(self, host='127.0.0.1', user=FTP_USER, passwd=FTP_PASSWORD, acct='', timeout=1000): ftplib.FTP.__init__(self, host, user, passwd, acct, timeout) print(self.getwelcome()) # 显示ftp服务器欢迎信息 self.__ftp_dirs = None self.__ftp_files = None def __update_path(self): # 分辨是文件还是文件夹 self.__ftp_dirs = [] self.__ftp_files = [] for file in self.nlst(): try: self.cwd(file) self.cwd('..') self.__ftp_dirs.append(file) except: self.__ftp_files.append(file) def get_path(self): self.__update_path() return self.__ftp_dirs, self.__ftp_files def download_files(self, ftp_files, local_path, blocksize=8192): for ftp_file in ftp_files: self.__download(ftp_file, local_path, blocksize) def upload_files(self, local_files, ftp_path, blocksize=8192): for local_file in local_files: self.__upload(local_file, ftp_path, blocksize) def __download(self, ftp_file, local_path, blocksize=8192): local_file = os.path.join(local_path, os.path.basename(ftp_file)) u8_ftp_file = ftp_file try: print('begin __downLoad ' + ftp_file) fp = open(local_file, 'wb') self.retrbinary('RETR %s' % (u8_ftp_file), fp.write, blocksize) fp.close() print('__download ' + ftp_file + ' is end') except Exception as e: print('error: ', e) print('__download ' + ftp_file + 'is fail') def __upload(self, local_file, ftp_path, blocksize=8192): try: print('begin __upload ' + local_file) u8_ftp_file = os.path.join(ftp_path, os.path.basename(local_file)) local_file_path = os.path.join(LOCAL_DIR, local_file) fp = open(local_file_path, 'rb') self.storbinary('STOR %s' % (u8_ftp_file), fp, blocksize) fp.close() print('__upload ' + local_file + ' is end') except Exception as e: print('error: ', e) print('__upload ' + local_file + 'is fail') if __name__ == '__main__': ftp = Ftp() # dirs, files = ftp.get_path() # for dir in dirs: # print(dir) # # print('................') # for file in files: # print(file) ftp.download_files(['001F7A31AC2C_1625466411.background_1.jpg'], LOCAL_DIR) # ftp.upload_files(['001F7A31AC2C_1625466411.background_1.jpg'], '/')
-
config.py
#!/usr/bin/python3 # -*- coding:utf-8 -*- # @Author : Charlie Zhang # @Email : charlie.zhang@wiwide.com # @Time : 2021/7/15 16:34 # @Version : 1.0 # @File : config.py # @Software : PyCharm import os BASE_DIR = os.path.dirname(os.path.abspath(__file__)) FTP_USER = 'prod' FTP_PASSWORD = 'wiwide123' FTP_PASSIVE_PORTS = range(20050, 20060) FTP_DIR = os.path.join(BASE_DIR, 'origin') LOCAL_DIR = os.path.join(BASE_DIR, 'local')
-
目录结构
ftp/ |-- client.py 客户端 |-- config.py 配置文件 |-- local 本地文件目录 | `-- 001F7A31AC2C_1625466411.background_1.jpg |-- origin 远程文件目录 `-- server.py 服务端
这篇关于python 搭建FTP服务上传下载文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25Python编程基础:变量与类型
- 2024-11-25Python编程基础与实践
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器