带登录认证的安全文件服务器
Project description
File Server
带登录认证的安全文件服务器
功能特性
- 用户认证系统 - 基于用户名/密码的登录认证,使用 bcrypt 加密存储密码
- 会话管理 - 基于 Cookie 的安全会话机制,1小时超时
- 多级目录浏览 - 支持子目录导航,面包屑路径显示
- 文件分类浏览 - 目录列表按类型分类展示:HTML、XMind、JSON 和其他文件
- 文件下载 - 支持任意文件下载,显示文件大小和修改时间
- 文件上传 - 支持多文件上传,拖拽上传,自动处理文件名冲突
- 文件搜索 - 实时搜索/过滤文件名和目录名
- 文件排序 - 支持按名称、大小或修改时间排序
- JSON 查看器 - JSON 文件语法高亮显示,支持复制和下载
- 用户管理 - 创建、删除、修改用户密码
安全特性
- bcrypt 密码加密(rounds=12)
- 安全会话 ID(secrets.token_hex)
- HttpOnly + SameSite Cookie
- 会话超时机制
- 访问控制(未认证用户重定向到登录页)
- 路径遍历防护(防止
../攻击) - 文件上传安全(文件名清理、大小限制 100MB)
- 用户数据文件权限限制(0o600)
- 服务器路径信息隐藏(防止信息泄露)
安装
pip install pyfileserv
或使用 pipx 安装(推荐):
pipx install pyfileserv
使用方法
1. 创建用户
pyfileserv user create
# 或指定用户名
pyfileserv user create -u admin
2. 启动服务器
pyfileserv
# 或指定端口
pyfileserv -p 9000
命令行参数
| 参数 | 说明 | 默认值 |
|---|---|---|
-p, --port |
服务器端口 | 8000 |
-d, --directory |
文件服务目录 | 当前目录 |
-c, --config-dir |
配置文件目录 | 脚本目录 |
-s, --static-dir |
静态文件目录 | 脚本目录 |
用户管理命令
# 创建用户
pyfileserv user create [-u 用户名]
# 列出用户
pyfileserv user list
# 删除用户
pyfileserv user delete [-u 用户名]
# 修改密码
pyfileserv user passwd [-u 用户名]
示例
# 在端口 9000 启动服务器
pyfileserv -p 9000
# 指定文件服务目录
pyfileserv -d /path/to/files
# 自定义配置和静态文件目录
pyfileserv -c /etc/pyfileserv -s /var/www/files
访问
启动后访问 http://localhost:8000 或局域网 IP 地址。
项目结构
pyfileserv/
├── .github/
│ └── workflows/
│ └── python-publish.yml # PyPI 自动发布工作流
├── src/file_server/
│ ├── __init__.py # 包入口
│ ├── __main__.py # CLI 入口
│ ├── config.py # 配置模块
│ ├── auth.py # 认证模块
│ ├── templates.py # HTML 模板
│ ├── handlers.py # HTTP 处理器
│ ├── server.py # 服务器启动
│ ├── cli.py # 命令行工具
│ └── static/
│ ├── logo.svg # Logo 图标
│ └── favicon.svg # Favicon 图标
├── tests/
│ ├── __init__.py # 测试包入口
│ ├── conftest.py # pytest 配置
│ ├── test_auth.py # 认证模块测试
│ ├── test_config.py # 配置模块测试
│ └── test_handlers.py # 处理器测试
├── CHANGELOG.md # 变更日志
├── pyproject.toml # 项目配置
├── release.py # 发布脚本
└── README.md # 文档
开发与发布
安装开发依赖
pip install -e ".[test]"
运行测试
pytest tests/ -v
构建
pip install build
python -m build
发布到 TestPyPI
pip install twine
python -m twine upload --repository testpypi dist/*
发布到 PyPI
python -m twine upload dist/*
要求
- Python >= 3.6
- bcrypt >= 4.0.0
- dataclasses (仅 Python 3.6 需要)
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyfileserv-0.6.1.tar.gz
(30.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfileserv-0.6.1.tar.gz.
File metadata
- Download URL: pyfileserv-0.6.1.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9991cc10ceba5e88d54ca880be2564dba9dcfab02cfeed86194cc2d20a6d562
|
|
| MD5 |
04823905432a7b6e625fbe318f874b22
|
|
| BLAKE2b-256 |
07bb118e6cfb10dc2b7098f566777a4aa782b527947ca470a6a5d86610c39967
|
File details
Details for the file pyfileserv-0.6.1-py3-none-any.whl.
File metadata
- Download URL: pyfileserv-0.6.1-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ce20515d949c1dba46e70d013add48052e3e421854c1e8b0ead95764b157ccb
|
|
| MD5 |
52477b532d136ce38e087b4ec722e76f
|
|
| BLAKE2b-256 |
a271a9881cae49b29fa976abde307020eb998ccb450e91fd9d94177e231b9f76
|