You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 07:26:10