【发布时间】:2020-05-24 16:30:37
【问题描述】:
我在 Windows 机器上使用 uvicorn 在 Python 中运行 FastAPI 应用程序。当我要么时它工作正常
- 在我的 Mac 上运行以下代码,或者
- 当我没有为 uvicorn 指定端口时(从 uvicorn.run 调用中删除
host参数) - 当我指定端口“127.0.0.1”时,它是我根本不指定主机时它使用的主机。
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='0.0.0.0')
当我在浏览器上转到 0.0.0.0:8080 时,我收到一条错误消息,提示“无法访问此站点”。
我已经检查了我当前的活动端口,以确保我没有使用 netstat -ao |find /i "listening" 发生冲突,并且 0.0.0.0:8080 未在使用中。
我当前的文件配置如下:
working_directory
└── app
├── gunicorn_conf.py
└── main.py
我的 gunicorn_conf.py 超级简单,只是尝试设置主机和端口:
host = "0.0.0.0"
port = "8080"
当我指定主机“0.0.0.0”时,我怎样才能让它工作?
【问题讨论】:
标签: python windows localhost fastapi uvicorn