有没有办法在IIS服务器上部署FastAPI?

问题描述 投票:0回答:2

我在 IIS 上部署 FastAPI 应用程序时遇到问题。

这是我得到的错误。

Error occurred:

Traceback (most recent call last):
    File "c:\programdata\anaconda3\lib\site-packages\wfastcgi.py", line 847, in main
        result = handler(record.params, response.start)
TypeError: __call__() missing 1 required positional argument: 'send'

有什么想法吗?

python-3.x iis deployment fastcgi fastapi
2个回答
0
投票

我通过

解决了这个问题

1-在cmd中pip安装a2wsgi

2- 在 main.py 中

  • A -
    from a2wsgi import ASGIMiddleware
  • B - 在代码末尾写入

wsgi_app = ASGIMiddleware(应用程序)

像这样

from a2wsgi import ASGIMiddleware
from fastapi import FastAPI


app = FastAPI()


@app.get("/")
async def view():
    return {"Hello":"World!"}

。 。 。 。 。 .

wsgi_app = ASGIMiddleware(app)

3-在webconfig中添加

<add key="WSGI_HANDLER" value="main.wsgi_app" />

0
投票

使用 HttpPlatform 处理程序在 IIS 上部署 FastAPI 应用程序

以下解决方案假设您的快速 api 应用程序在应用程序的根目录中使用

python -m venv venv
创建了一个虚拟环境。我使用了 uvicorn 但类似的参数也适用于 hypercorn

网络配置

<configuration>
<system.webServer>
    <handlers>
        <add name="fastapi_app_handler" 
        path="*" 
        verb="*" 
        modules="httpPlatformHandler" 
        scriptProcessor="C:\fastapi_app\venv\Scripts\python.exe" 
        resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <httpPlatform processPath="C:\fastapi_app\venv\Scripts\python.exe" 
    arguments="-m uvicorn main:app --host 0.0.0.0 --port %HTTP_PLATFORM_PORT%" 
    stdoutLogEnabled="true" stdoutLogFile="C:\fastapi_app\iis_logs\python-stdout.log">
        <environmentVariables>
            <environmentVariable name="SERVER_PORT" value="HTTP_PLATFORM_PORT" />
        </environmentVariables>
    </httpPlatform>
</system.webServer>

主.py

import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    a = "a"
    b = "b" + a
    return {"hello world": b}

if __name__ == "__main__":
    uvicorn.run('main:app',reload=True)

目录:

IIS 管理器

您可以通过 IIS 管理器检查相应站点的 Handler Mappings,以查看您的处理程序是否在列表中,在此示例中,处理程序为“fastapi_app_handler”。在 IIS 中部署网站的一般步骤可以在此处找到。

© www.soinside.com 2019 - 2024. All rights reserved.