Search code examples
pythonfastapi

Errno 13 error while attempting to bind on address ('127.0.0.1', 8000)


I'm doing a tutorial on FastAPI and I've encountered this 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

I first started with the code (I'm using Visual Studio):

from fastapi import FastAPI

app = FastAPI()

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

No errors. Then, I've tried a command below and got the error mentioned above:

(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.

I've tried changing python.exe properties: python.exe properties > compatibility > start as admin but then, I've received error while running python code: error message

and a message after trying uvicorn main:app:

(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'

I've changed python.exe properties back and tried to kill the process on port 8000 (found PID, which was 3680)

(venv) C:\VS_projects\fastapi>taskkill /f /pid 3680
ERROR: The process with PID 3680 could not be terminated.
Reason: Access denied.

Also, I don't have antivirus. Please help


Solution

  • If you don't need to run specifically on port 8000, you can specify another unused port with:

    uvicorn main:app --port 8070
    

    If you need port 8000, did you try to run your command to kill the process as sudo ?
    Open a shell with "Run as Administrator" and run:

    taskkill /f /pid 3680
    

    This should get rid of the running process on port 8000 and allow you to run your fast api application.