Python 快速开发Web文件服务器
 ### **一、基于标准库的实现(python自带零依赖)** 使用**Python**内置的**http.server**和**cgi**模块实现,适合快速部署内网文件共享服务。 ``` import http.server import cgi import os import sys from urllib.parse import urlparse, unquote class FileHandler(http.server.BaseHTTPRequestHandler): UPLOAD_DIR = 'uploads' # 上传目录配置 def send_html_header(self): """发送 HTML 响应头""" self.send_response(200) # 设置响应状态码为 200(OK) self.send_header('Content-type', 'text/html; charset=utf-8') # 设置响应内容类型为 HTML self.end_headers() # 结束响应头 def do_POST(self): # 解析表单数据(含文件) form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'} ) # 获取上传文件并保存 file_item = form['file'] if file_item.filename: file_path = os.path.join(self.UPLOAD_DIR, os.path.basename(file_item.filename)) with open(file_path, 'wb') as f: f.write(file_item.file.read()) self.send_redirect('/') # 上传后返回首页 def do_GET(self): # 文件下载处理 if self.path.startswith('/download/'): file_path = os.path.join(os.getcwd(), unquote(self.path[9:])) if os.path.isfile(file_path): self.send_header('Content-Disposition', f'attachment; filename="{os.path.basename(file_path)}"') with open(file_path, 'rb') as f: self.wfile.write(f.read()) # 显示文件列表 else: self.send_html_header() self.wfile.write(self.generate_file_list().encode('utf-8')) def generate_file_list(self): """生成带上传表单的文件列表HTML""" files = os.listdir(self.UPLOAD_DIR) html = f""" 文件列表 {"".join(f'{f}' for f in files)} """ return html if __name__ == '__main__': os.makedirs(FileHandler.UPLOAD_DIR, exist_ok=True) port = 8000 if len(sys.argv) ') def download_file(filename): try: return send_from_directory( app.config['UPLOAD_FOLDER'], filename, as_attachment=True ) except FileNotFoundError: abort(404) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True) ``` 配套模板文件**(templates/index.html):** ``` 文件服务器 body { font-family: Arial, sans-serif; margin: 20px; } table { width: 100%; border-collapse: collapse; } th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } th { background-color: #f2f2f2; } .upload-form { margin: 20px 0; } 文件服务器 上传文件 上传 文件列表 文件名 大小 (字节) 修改时间 操作 {% for file in files %} {{ file.name }} {{ file.size }} {{ file.modified|datetime }} 下载 {% endfor %} ``` **使用说明:** 1、安装库:**pip install flask werkzeug** 2、运行服务:**python file_server.py** 3、访问地址:**http://localhost:5000** 4、扩展: **1)安全增强**(在**Flask**中实现) ``` # 文件类型白名单 ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png'} def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS # 在上传路由中添加检查 if file and allowed_file(file.filename): file.save(...) else: return "文件类型不允许", 400 ``` **2)大文件分块上传**(使用**Flask**) ``` @app.route('/chunk_upload', methods=['POST']) def chunk_upload(): chunk = request.files['chunk'] chunk_number = request.form['chunkNumber'] filename = request.form['filename'] # 创建临时目录存储分块 temp_dir = os.path.join(UPLOAD_FOLDER, 'temp', filename) os.makedirs(temp_dir, exist_ok=True) # 保存分块 chunk.save(os.path.join(temp_dir, chunk_number)) # 合并检测(当收到最后一块时) if int(chunk_number) == int(request.form['totalChunks']) - 1: merge_chunks(filename) return 'OK' ``` **3)添加基础认证**(使用装饰器) ``` from functools import wraps def auth_required(f): @wraps(f) def decorated(*args, **kwargs): auth = request.authorization if auth and auth.username == 'admin' and auth.password == 'secret': return f(*args, **kwargs) return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'}) return decorated # 在路由上添加装饰器 @app.route('/') @auth_required def index(): ... ``` ### **4)部署建议** **(1)开发环境**:直接运行**Flask**应用**(app.run())** **(2)生产环境**: - 使用**Gunicorn**部署:**gunicorn -w 4 app:app** - 配置**Nginx**反向代理(增加文件上传大小限制): ``` server { listen 80; client_max_body_size 100M; # 允许100MB大文件 location / { proxy_pass http://localhost:8000; } } ``` **(3)安全配置**: - 禁用目录遍历:在文件列表生成时过滤敏感文件。 - 设置文件大小上限:**app.config[’MAX_CONTENT_LENGTH’] = 100 * 1024 * 1024。** - 定期清理上传目录。 标准库**http.server**方案适合快速临时、内网快速共享,**Flask**方案更适合长期稳定运行的服务,适合生产环境/复杂需求。可根据需求灵活选择。 **“无他,惟手熟尔”!**有需要的用起来。 **如果你觉得这篇文章有用,欢迎点赞、转发、收藏、留言、推荐**❤!