Python FTP服务器
安装模块:
pip install pyftpdlib
实例:
#-*- coding:utf-8 -*-
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os
# import getpass
def getIP():
'''
通过执行命令:ipconfig,获取当前电脑的IP地址
'''
con = os.popen('ipconfig').read()
lines = con.split('\n')
for line in lines:
if 'IPv4 地址' in line or 'IPv4 Address' in line:
ip = line.split()[-1]
return ip
def ftpServer(ip,port,name,password,path,permission):
print('=========================== Address: ftp://%s:%s =======================\n' % (ip, port))
#实例化虚拟用户,这是FTP验证首要条件
authorizer = DummyAuthorizer()
#添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
authorizer.add_user(name, password, path, perm=permission)
# #添加匿名用户,赋予匿名用户可以访问的目录
# authorizer.add_anonymous('E:/')
#初始化ftp句柄
handler = FTPHandler
handler.authorizer = authorizer
# 被动连接模式
#handler.passive_ports = range(60000, 65535)
#监听ip 和 端口
server = FTPServer((ip,port), handler)
# 设置连接限制
# server.max_cons = 256
# server.max_cons_per_ip = 5
#开始服务
server.serve_forever()
if __name__ == '__main__':
ip = getIP()
path = input('Path:')
if path == '':
path = os.getcwd()+'\\'+'FTP'
if not os.path.exists(path):
os.mkdir(path)
# 默认使用8888端口
port = 8888
# 可以隐藏输入的密码
# password = getpass.getpass('Password:')
ftpServer(ip,port,name='wzt',password='wztshine',path=path,permission='elradfmwMT')
''' 备注:权限列表
Read permissions:
- "e" = change directory (CWD command)
- "l" = list files (LIST, NLST, STAT, MLSD, MLST, SIZE, MDTM commands)
- "r" = retrieve file from the server (RETR command)
Write permissions:
- "a" = append data to an existing file (APPE command)
- "d" = delete file or directory (DELE, RMD commands)
- "f" = rename file or directory (RNFR, RNTO commands)
- "m" = create directory (MKD command)
- "w" = store a file to the server (STOR, STOU commands)
- "M" = change file mode (SITE CHMOD command)
- "T" = update file last modified time (MFMT command)
'''
分类:
Python Scripts
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构