From b940c8dc7422c642d84ff3f95f9fe84402a05dc6 Mon Sep 17 00:00:00 2001 From: pzf8uwcp6 <13519963156@163.com> Date: Fri, 11 Oct 2024 19:19:45 +0800 Subject: [PATCH] ADD file via upload --- server.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..b9fd02e --- /dev/null +++ b/server.py @@ -0,0 +1,26 @@ +#server.py +from fastapi import FastAPI +from fastapi.staticfiles import StaticFiles +from fastapi.responses import HTMLResponse +import os + +app = FastAPI() + +# Configure static files +app.mount("/static", StaticFiles(directory="D:/VS/.vscode/Web"), name="static") + +@app.get("/", response_class=HTMLResponse) +async def read_root(): + file_path = 'D:/VS/.vscode/Web/web.html' + print(os.path.exists(file_path)) + try: + with open(file_path, 'r', encoding='utf-8') as f: + return f.read() + except FileNotFoundError: + return HTMLResponse(content="404 Not Found: The requested file was not found.", status_code=404) + except Exception as e: + return HTMLResponse(content=f"500 Internal Server Error: {str(e)}", status_code=500) + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=3000) \ No newline at end of file