# Web服务器 **Repository Path**: fang-jinchen/web-server ## Basic Information - **Project Name**: Web服务器 - **Description**: 简单的web服务器 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-06-05 - **Last Updated**: 2025-06-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Web服务器 #### 介绍 这是一个使用Python实现的简单Web服务器,支持IP白名单访问控制、静态文件服务和基本的安全防护功能。 ## 功能特性 - 静态文件服务(HTML, 图片, CSS, JS等) - 多线程处理客户端请求 - IP白名单访问控制(支持CIDR表示法) - 防止路径遍历攻击 - 访问日志记录 - 自动内容类型识别 - 错误处理(404 Not Found, 403 Forbidden, 500 Internal Server Error) ## 项目结构 ``` simple_web_server/ ├── server.py # 主服务器程序 ├── config.py # 配置文件 ├── access_control.py # 访问控制模块 ├── static/ # 静态文件目录(默认根目录) │ ├── index.html # 默认首页 │ ├── about.html # 示例页面 │ └── image.jpg # 示例图片 ├── logs/ # 日志目录(自动创建) │ └── access.log # 访问日志 └── README.md # 项目说明文件 ``` ## 依赖 - Python 3.6+ ## 快速开始 1. 克隆或下载项目 2. 进入项目目录 3. 启动服务器: ```bash python server.py ``` 4. 在浏览器中访问:http://localhost:8080 ## 配置说明 编辑`config.py`文件进行配置: ```python # 服务器配置 HOST = '0.0.0.0' # 监听所有接口 PORT = 8080 # 监听端口 ROOT_DIR = 'static' # 静态文件根目录 LOG_FILE = 'logs/access.log' # 访问日志路径 # 访问控制配置 ACCESS_CONTROL_ENABLED = True # 是否启用访问控制 ALLOWED_IPS = [ '127.0.0.1', # 本地主机 '::1', # IPv6本地主机 '192.168.1.0/24', # 整个192.168.1.x网段 '10.0.0.0/8', # 10.x.x.x私有网络 ] # 内容类型映射 CONTENT_TYPES = { '.html': 'text/html', '.txt': 'text/plain', '.jpg': 'image/jpeg', # ... 其他类型 } ``` ## 测试方法 1. **正常访问测试**: - 访问 http://localhost:8080 查看首页 - 访问 http://localhost:8080/about.html 查看示例页面 2. **错误页面测试**: - 访问 http://localhost:8080/missing.html 测试404错误 3. **安全防护测试**: - 访问 http://localhost:8080/../secret.txt 测试路径遍历攻击(应被阻止) 4. **访问控制测试**: - 从非白名单IP访问服务器(应返回403 Forbidden) ## 日志查看 所有访问记录将同时输出到控制台并保存到`logs/access.log`文件,格式如下: ``` 2025-06-05 14:30:25 - 127.0.0.1 - /index.html - 200 OK 2025-06-05 14:30:30 - 192.168.1.100 - /about.html - 200 OK 2025-06-05 14:30:35 - 10.0.0.1 - /secret.html - 403 Forbidden ``` ## 扩展建议 - 添加HTTPS支持 - 实现用户认证(Basic Auth) - 添加文件上传功能 - 支持CGI脚本 - 添加目录列表功能 ## 许可证 本项目使用 [MIT License](LICENSE)。 ```