python3 搭建 ftp服务器的两种方法

燕富
2023-12-01

 

方法一:(只能通过工具登入)

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.log import LogFormatter
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
fh = logging.FileHandler(filename='myftpserver.log', encoding='utf-8') # 默认追加到日志文件
ch.setFormatter(LogFormatter())
fh.setFormatter(LogFormatter())
logger.addHandler(ch)  # 将日志输出到屏幕
logger.addHandler(fh)  # 将日志输出至文件

authorizer = DummyAuthorizer()
# 添加用户权限与路径,参数(用户名,密码,用户目录,权限(见说明))
authorizer.add_user('lanlang', '123456', "./", perm="elradfmw")
# 添加匿名用户,只添加路径
authorizer.add_anonymous("./", perm="elradfmw")

handler = FTPHandler
handler.authorizer = authorizer
# 添加被动端口范围
handler.passive_ports = range(2000, 2333)
# 下载上传速度设置
dtp_handler = ThrottledDTPHandler
dtp_handler.read_limit = 300 * 1024 # 300kb/s
dtp_handler.write_limit = 300 * 1024 # 300kb/s
handler.dtp_handler = dtp_handler
# 监听IP和端口,Linux中需要root用户才能使用21端口
server = FTPServer(('0.0.0.0', 8888), handler)
# 最大连接数
server.max_cons = 150
server.max_cons_per_ip = 15

server.serve_forever()

方法二:(只能通过浏览器访问)

 python3 -m http.server 8090

 类似资料: