Azure 应用服务 FastApi 服务器未在指定端口上运行

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

我正在 Azure 应用服务上运行 FastApi 服务器。
这是通过将代码链接到 github(而不是使用容器)来实现的。
我已将启动命令指定为:

python -m uvicorn routes:app --host 0.0.0.0 --port 8000
当我在浏览器中运行输入时:https://fastapi-sappv01.azurewebsites.net:8000/我收到错误

它给出错误

我还有一个 Streamlit 前端,可以向服务器发送请求。

requests.exceptions.ConnectionError: HTTPSConnectionPool(主机='stock-app2.azurewebsites.net',端口=8080): url: /symbols 超出了最大重试次数(由以下原因引起) NameResolutionError(": 无法解析 'stock-app2.azurewebsites.net' ([Errno 8] 节点名称或服务名称已提供,或未知)”))

问题
1:如何调试问题?
2:如何查看服务器端日志? (在代码中打印语句?)
3:如果我不指定--port参数,我应该期望服务器运行哪个端口?

azure-web-app-service fastapi
1个回答
0
投票

我创建了一个简单的 Web 应用程序,使用 FastAPI 后端和 Streamlit 作为前端。已成功部署到Azure。

为了部署 FastAPI 和 Streamlit,我在 Azure 门户中创建了两个 Web 应用程序。

下面是我的后端和前端的完整代码。

后端/main.py

from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class Item(BaseModel):
    name: str
    description: str = None
    price: float
    tax: float = None
@app.get("/")
def read_root():
    return {"message": "Welcome from the API"}
@app.post("/items")
async def create_item(item: Item):
    return item
if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8080)

后端/requirements.txt

fastapi
uvicorn

确保后端 URL 在前端中是正确的。

前端/streamlit_app.py

import streamlit as st
import requests
st.title('FastAPI - Streamlit Integration')
def create_item(item_data):
    url = "https://kapythonapi.azurewebsites.net/items"
    response = requests.post(url, json=item_data)
    return response.json()
st.sidebar.title('Create Item')
name = st.sidebar.text_input('Name')
description = st.sidebar.text_area('Description')
price = st.sidebar.number_input('Price')
tax = st.sidebar.number_input('Tax')
if st.sidebar.button('Create'):
    item_data = {"name": name, "description": description, "price": price, "tax": tax}
    response = create_item(item_data)
    st.write('Response from backend:', response)

前端/requirements.txt

streamlit

将前端和后端部署到 Azure 应用服务后,在 Azure Web 应用的“配置”部分中配置启动命令,如下所示。

对于 Fastapi:

 gunicorn --worker-class uvicorn.workers.UvicornWorker --timeout 600 --access-logfile '-' --error-logfile '-' main:app

对于 Streamlit 应用程序:

python -m streamlit run <YourFileName>.py --server.port 8000 --server.address 0.0.0.0

enter image description here

enter image description here

如果遇到任何 CORS 错误,请在 FastAPI 后端应用程序中启用 CORS,如下所示。

enter image description here

Azure 应用服务输出

enter image description here

enter image description here

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