当前位置: 首页 > 知识库问答 >
问题:

如何运行FastAPI/Uvicorn在谷歌可乐?

萧晔
2023-03-14

我试图在谷歌可乐上运行一个“本地”网络应用程序,使用FastAPI/Uvicorn,就像我见过的一些烧瓶应用程序示例代码一样,但无法让它工作。有人能做到这一点吗?感谢它。

!pip install FastAPI -q
!pip install uvicorn -q
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}
#attempt 1
if __name__ == "__main__":
    uvicorn.run("/content/fastapi_002:app", host="127.0.0.1", port=5000, log_level="info")
#attempt 2
#uvicorn main:app --reload
!uvicorn "/content/fastapi_001.ipynb:app" --reload

共有1个答案

阎晗日
2023-03-14

您可以使用ngrok将端口导出为外部url。基本上,ngrok在本地主机上获取可用/托管的内容,并使用临时公共URL将其公开到internet。

首先安装依赖项

!pip install fastapi nest-asyncio pyngrok uvicorn

创建你的应用程序

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

@app.get('/')
async def root():
    return {'hello': 'world'}

然后运行它。

import nest_asyncio
from pyngrok import ngrok
import uvicorn

ngrok_tunnel = ngrok.connect(8000)
print('Public URL:', ngrok_tunnel.public_url)
nest_asyncio.apply()
uvicorn.run(app, port=8000)
 类似资料:
  • 我创建了一个个人使用的基本应用程序。我的应用程序的支持使用快速Api和SQLite数据库。通常要运行我的启动和运行我的后端服务器,我必须使用以下命令: 我以前见过其他人创建python可执行文件。我也想这样做,但我需要它来启动uvicorn服务器。如何创建运行uvicorn服务器的python可执行文件? 还是只编写一个执行此操作的批处理脚本更好?

  • Supported tags and respective Dockerfile links python3.9, latest (Dockerfile) python3.8, (Dockerfile) python3.7, (Dockerfile) python3.6 (Dockerfile) python3.9-slim (Dockerfile) python3.8-slim (Dockerf

  • 我有一个本地运行的服务器。当我在AWS EC2上运行它并在8000端口上从外部发送请求时,我得到以下错误: 如果您能告诉我如何在端口80上执行此操作,那将非常好。

  • 我正在学习Fastapi,我正在localhost启动一个uvicorn服务器。每当出现错误/异常时,我都不会得到回溯。所有我得到的是: 所以,调试很困难,我正在试用python的日志模块 我还尝试过使用调试参数启动uvicorn

  • 我的FastAPI应用程序似乎记录了很多事情两次。 这包括引发的任何异常,您将两次获得整个堆栈跟踪。我已经看到一些答案建议删除Uvicorn的日志处理程序,但这感觉是错误的。如果在堆栈的Uvicorn层发生日志事件,但在FastAPI中没有,该怎么办? 有没有一种方法可以只获取一次日志输出,而不只是覆盖uvicorn的日志处理程序?

  • 我承认我以前从未使用过。当我运行命令给出错误: