我是使用 fastapi 的新手。我在 docker 容器中有一个 main.py。当我使用连接到 fastapi 时
\nuvicorn main:app \xe2\x80\x94-reload \nRun Code Online (Sandbox Code Playgroud)\n来自我的容器。系统提示我连接到http://127.0.0.1:8000。将地址复制到 Firefox 时出现错误:
\n unable to connect. \nRun Code Online (Sandbox Code Playgroud)\n如何连接到 fastapi 服务器?
\nPS 我正在工作的 git 分支是由另一位同事开发的,所以我几乎不知道 fastapi 是如何在 docker 内设置的
\n我第一次使用 FastAPI 开发一个小型 API。我用来uvicorn运行该应用程序。
当我使用时:
$ uvicorn main:app --host 0.0.0.0
Run Code Online (Sandbox Code Playgroud)
我可以从网络内部(使用我的公共 IP)访问该应用程序,但不能从外部访问该应用程序。我已经检查了防火墙,甚至尝试完全禁用它,但是没有任何效果。我只是希望能够向外界展示该应用程序。我怎样才能做到这一点?
我根据官方文档使用fastapi上传文件,就像:
@app.post("/create_file/")
async def create_file(file: UploadFile=File(...)):
file2store = await file.read()
# some code to store the BytesIO(file2store) to the other database
Run Code Online (Sandbox Code Playgroud)
当我使用 python requests lib 发送请求时:
f = open(".../file.txt", 'rb')
files = {"file": (f.name, f, "multipart/form-data")}
requests.post(url="SERVER_URL/create_file", files=files)
Run Code Online (Sandbox Code Playgroud)
file2store 始终为空。有时(很少见),它可以获取文件字节,但几乎所有时间都是空的,所以我无法在另一个数据库上恢复文件。我还尝试了 'bytes' 而不是 'UploadFile',我得到了相同的结果。我的代码有什么地方不对,还是我使用fastapi上传文件的方式有问题?我google了很长时间,但没有成功。所以我在这里提出问题,希望知道答案的人可以帮助我。谢谢
我在 Windows 机器上使用 uvicorn 在 Python 中运行 FastAPI 应用程序。当我要么
host从 uvicorn.run 调用中删除参数)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')
Run Code Online (Sandbox Code Playgroud)
当我在浏览器上转到 0.0.0.0:8080 时,收到一条错误消息,提示“无法访问此站点”。
我已经检查了我当前的活动端口,以确保我没有使用冲突netstat -ao |find /i "listening"并且 0.0.0.0:8080 没有被使用。
我当前的文件配置如下所示:
working_directory
??? app
??? gunicorn_conf.py
??? main.py
Run Code Online (Sandbox Code Playgroud)
我的 gunicorn_conf.py 非常简单,只是尝试设置主机和端口:
host = "0.0.0.0"
port = "8080"
Run Code Online (Sandbox Code Playgroud)
当我指定主机“0.0.0.0”时,如何使其工作?
为什么 FastAPI 不将 cookie 返回到我的前端(这是一个 React 应用程序)?
这是我的代码:
@router.post("/login")
def user_login(response: Response,username :str = Form(),password :str = Form(),db: Session = Depends(get_db)):
user = db.query(models.User).filter(models.User.mobile_number==username).first()
if not user:
raise HTTPException(400, detail='wrong phone number or password')
if not verify_password(password, user.password):
raise HTTPException(400, detail='wrong phone number or password')
access_token = create_access_token(data={"sub": user.mobile_number})
response.set_cookie(key="fakesession", value="fake-cookie-session-value") #here I am set cookie
return {"status":"success"}
Run Code Online (Sandbox Code Playgroud)
当我从 Swagger UI autodocs 登录时,我可以使用 Chrome 浏览器上的 DevTools 在响应标头中看到 cookie。但是,当我从 React 应用程序登录时,没有返回 cookie。我正在使用 axios 发送这样的请求:
await axios.post(login_url, formdata)
具体来说,我希望以下示例能够正常工作:
from typing import List
from pydantic import BaseModel
from fastapi import FastAPI, UploadFile, File
app = FastAPI()
class DataConfiguration(BaseModel):
textColumnNames: List[str]
idColumn: str
@app.post("/data")
async def data(dataConfiguration: DataConfiguration,
csvFile: UploadFile = File(...)):
pass
# read requested id and text columns from csvFile
Run Code Online (Sandbox Code Playgroud)
如果这不是 POST 请求的正确方法,请告诉我如何从 FastAPI 中上传的 CSV 文件中选择所需的列。
我有一个简单的 React Ui,它应该从中获取 json 文件localhost:8000/todo并在localhost:3000. 这是所需的输出:

所以,这两行是“读一本书”。和“骑自行车环城”。没有显示。这两行应该来自localhost:8000/todo类型JSON信息。我觉得我可以从 获取数据localhost:8000/todo,但我不知道如何在 中显示它们localhost:3000,这是我的输出。
这是我为此提供的功能:
export default function Todos() {
const [todos, setTodos] = useState([])
const fetchTodos = async () => {
const response = await fetch("http://localhost:8000/todo")
const todos = await response.json()
setTodos(todos.data)
}
useEffect(() => {
fetchTodos()
}, [])
return (
<TodosContext.Provider value={{todos, fetchTodos}}>
<AddTodo />
<Stack spacing={5}>
{todos.map((todo) => (
<b>{todo.item}</b>
))}
</Stack>
</TodosContext.Provider>
)
}
Run Code Online (Sandbox Code Playgroud)
{todos.item}是应该打印项目的部分,但它没有!
以下是 …
fastapi ×7
python ×6
reactjs ×2
axios ×1
connection ×1
cors ×1
file-upload ×1
http ×1
http-post ×1
javascript ×1
localhost ×1
networking ×1
pydantic ×1
uvicorn ×1
windows ×1