如何在IIS10中运行启用WebSocket的FastAPI?
IIS 10部署FastAPI后WebSocket无法正常使用
已成功在IIS 10上部署运行FastAPI,但WebSocket功能异常,客户端始终提示错误:WebSocket is already in CLOSING or CLOSED state.
当前web.config配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/> </handlers> <httpPlatform processPath="C:\Public-T\Python\VenvFastapi\Scripts\uvicorn.exe" arguments="main:app --host 0.0.0.0 --port %HTTP_PLATFORM_PORT%" stdoutLogEnabled="false" stdoutLogFile="D:\WebSite\FastApi\logs\uvicorn" startupTimeLimit="60" processesPerApplication="16"> <environmentVariables> <environmentVariable name="PYTHONPATH" value="D:\WebSite\FastApi\" /> <environmentVariable name="HTTP_PLATFORM_PORT" value="%HTTP_PLATFORM_PORT%" /> </environmentVariables> </httpPlatform> </system.webServer> </configuration>
WebSocket代码(单独运行正常)
@app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() while True: data = await websocket.receive_text() await websocket.send_text(f"Message text was: {data}")
内容的提问来源于stack exchange,提问作者user2393163
相关产品推荐
相关产品推荐

