base_ftp_server.py 主入口
# coding:utf-8
import logging
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import ThrottledDTPHandler, FTPHandler
from pyftpdlib.servers import FTPServer
# 用于读取配置文件
from config_ftp import *
def init_ftp_server():
authorizer = DummyAuthorizer()
"""
读权限:
- "e" = 改变文件目录
- "l" = 列出文件 (LIST, NLST, STAT, MLSD, MLST, SIZE, MDTM commands)
- "r" = 从服务器接收文件 (RETR command)
写权限:
- "a" = 文件上传 (APPE command)
- "d" = 删除文件 (DELE, RMD commands)
- "f" = 文件重命名 (RNFR, RNTO commands)
- "m" = 创建文件 (MKD command)
- "w" = 写权限 (STOR, STOU commands)
- "M" = 文件传输模式 (SITE CHMOD command)
"""
if enable_anonymous:
# 添加匿名用户
authorizer.add_anonymous(anonymous_path)
# 读取配置中用户并授权
for user in user_list:
name,passwd,permit,homedir= user
try:
authorizer.add_user(name, passwd, homedir, perm=permit)
except:
print("配置文件错误请检查是否正确匹配了相应的用户名、密码、权限、路径")
print(user)
dtp_handler = ThrottledDTPHandler
# 上传速度 下载速度
dtp_handler.read_limit = max_download
dtp_handler.write_limit = max_upload
# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
# 是否开启记录
if enable_logging:
logging.basicConfig(filename='pyftp.log', level=logging.INFO)
# 登录时候显示的标题
handler.banner = welcom_banner
handler.masquerade_address = masquerade_address
# 主动模式和被动模式
handler.passive_ports = range(passive_ports[0], passive_ports[1])
# 监听的ip和端口
address = (ip, port)
server = FTPServer(address, handler)
# 设置最大连接数
server.max_cons = max_cons
server.max_cons_per_ip = max_pre_ip
# 开启ftp
server.serve_forever()
def ignor_octothrpe(text):
for x, item in enumerate(text):
if item == "#":
return text[:x]
pass
return text
def init_user_config():
f = open("baseftp.ini",encoding='utf-8')
while 1:
line = f.readline()
if len(ignor_octothrpe(line)) > 3:
user_list.append(line.split())
# todo
if not line:
break
if __name__ == '__main__':
# 用于保存授权用户的登录
user_list = []
# 从配置文件初始化用户
init_user_config()
init_ftp_server()
baseftp.ini 配置
# 账号 密码 权限 路径
root 123 #### /home/test
config_ftp.py
ip = "0.0.0.0"
port = "21"
# 上传速度 300 Kb/sec (300 * 1024)
max_upload = 300 * 1024
#下载速度 300 Kb/sec (300 * 1024)
max_download = 300 * 1024
# 最大连接数
max_cons = 256
# 最多ip连接数
max_pre_ip = 10
# 被动连接端口 这个必须比客户端连接数多否者客户端不能连接
passive_ports = (2223, 2233)
# 是否允许匿名访问
enable_anonymous = False
# 打开记录? 默认False
enable_logging = False
# 日志记录文件名称
logging_name = r"pyftp.log"
#公网ip
masquerade_address ="127.0.0.1"
# 添加欢迎标题 主要是使用终端登录的查看用户
welcom_banner = r"Welcome to private ftp."
默认的匿名用户路径
anonymous_path = r"/home/"