# -*- coding: utf-8 -*- from fastapi import FastAPI, HTTPException from fastapi.responses import FileResponse from pathlib import Path import os app = FastAPI() # 假设你的XML文件文件夹路径是 "xml_file" xml_folder_path = Path("/Users/jack/source/mySpace/mycode/my_project/spider_rss/xml_file") @app.get("/{filename}") async def read_xml(filename: str): # 构建完整的文件路径 file_path = xml_folder_path / f"{filename}.xml" # 检查文件是否存在 if not file_path.is_file(): raise HTTPException(status_code=404, detail="File not found") # 返回文件响应 return FileResponse(file_path, media_type="application/xml") if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)