You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI启动遇Errno 13:端口8000绑定权限问题求助

问题:FastAPI启动时端口8000绑定权限错误

错误信息

[Errno 13] error while attempting to bind on address ('127.0.0.1', 8000): an attempt was made to access a socket in a way forbidden by its access permissions

环境与代码

使用Visual Studio开发,代码无语法错误:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "sth sth"}

执行命令及错误输出

执行启动命令:

(venv) C:\VS_projects\fastapi>uvicorn main:app

得到错误输出:

INFO:     Started server process [1524]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
ERROR:    [Errno 13] error while attempting to bind on address ('127.0.0.1', 8000): an attempt was made to access a socket in a way forbidden by its access permissions
przypisane do niego uprawnienia dostępu
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.

已尝试的解决方法及失败情况

  • 修改python.exe属性,设置“兼容性>以管理员身份运行”,执行命令时出现:
    (venv) C:\VS_projects\fastapi>uvicorn main:app
    Unable to create process using '"C:\Users\mmm\AppData\Local\Programs\Python\Python312\python.exe" "C:\VS_projects\fastapi\venv\Scripts\uvicorn.exe" main:app'
    
  • 改回python.exe属性后,尝试终止端口8000上的进程(PID 3680),失败:
    (venv) C:\VS_projects\fastapi>taskkill /f /pid 3680
    ERROR: The process with PID 3680 could not be terminated.
    Reason: Access denied.
    
  • 确认未安装杀毒软件

解决方案

1. 换用非特权端口启动

Windows系统中1024以下端口需要管理员权限,直接切换到8000以外的端口(如8001)启动服务:

uvicorn main:app --port 8001

2. 以管理员身份运行终端

  • 右键点击命令提示符/PowerShell,选择「以管理员身份运行」
  • 激活虚拟环境:
    C:\VS_projects\fastapi\venv\Scripts\activate
    
  • 执行启动命令:
    uvicorn main:app
    

3. 强制终止占用端口的进程(管理员权限)

以管理员身份打开终端,先定位占用8000端口的进程:

netstat -ano | findstr :8000

找到对应PID后,执行强制终止命令:

taskkill /f /pid <替换为实际PID>

若仍无法终止,大概率是系统进程占用,建议直接换端口或重启电脑后再尝试。

4. 检查虚拟环境权限并重建

  • 确保虚拟环境所在目录C:\VS_projects\fastapi的权限设置为当前用户可读写
  • 重新创建虚拟环境:
    python -m venv venv_new
    venv_new\Scripts\activate
    pip install fastapi uvicorn
    uvicorn main:app
    

内容的提问来源于stack exchange,提问作者Minka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 12:10:04