pyftpdlib库实现简单认证服务器

本文介绍如何使用Python构建一个功能丰富的FTP服务器,包括用户权限管理、速度限制及日志记录等功能。通过阅读本文,读者可以了解到如何配置服务器以支持匿名访问,并能够理解FTP服务器的基本工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值