Wednesday, February 7, 2024

[SOLVED] Address already in use - FastAPI

Issue

I keep getting [Errno 98] Address already in use But the address is not in use. I tried to change the ip and port but It isn't budging.


from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def main():
return {"message": "Helloworld,FastAPI"}


if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)


uvicorn main:app --reload also tried uvicorn main:app --host=172.0.0.2 --port=5000 then it gives [Errno 99] error while attempting to bind on address ('172.0.0.2', 5000): cannot assign requested address I tried running a flask dev server and it was also running on 172.0.0.1 without a problem?

using Arch-Manjaro-Linux

I used nmap to see what the fuss was about.

But only 2 ports in use on the 127.0.0.1 IP

PORT     STATE SERVICE
631/tcp  open  ipp
8000/tcp open  http-alt

I would use another IP and port but it gives an error that it can't be assigned.


Solution

Basically, you can do this. This will kill the process that listens TCP connections on port 8000

sudo lsof -t -i tcp:8000 | xargs kill -9


Answered By - Yagiz Degirmenci
Answer Checked By - Katrina (WPSolving Volunteer)