localhost:5000 unavailable in macOS v12 (Monterey)

Question:

I cannot access a web server on localhost port 5000 on macOS v12 (Monterey) (Flask or any other).

E.g., use the built-in HTTP server, I cannot get onto port 5000:

python3 -m http.server 5000

... (stack trace)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 48] Address already in use

If you have Flask installed and you run the Flask web server, it does not fail on start. Let’s take the minimum Flask example code:

# Save as hello.py in the current working directory.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Then run it (provided you have Flask/Python 3 installed):

export FLASK_APP=hello
flask run

Output:

* Running on http://127.0.0.1:5000/

However, if you try to access this server (from a browser or with anything else), it is denied:

curl -I localhost:5000
HTTP/1.1 403 Forbidden
Content-Length: 0
Server: AirTunes/595.13.1
Asked By: noname

||

Answers:

macOS Monterey introduced AirPlay Receiver running on port 5000. This prevents your web server from serving on port 5000. Receiver already has the port.

You can either:

  1. turn off AirPlay Receiver, or;
  2. run the server on a different port (normally best).

Turn off AirPlay Receiver

Go to System PreferencesSharingUntick Airplay Receiver.

Enter image description here

See more details

You should be able to rerun the server now on port 5000 and get a response:

python3 -m http.server 5000

Serving HTTP on :: port 5000 (http://[::]:5000/) ...

Run the server on a different port than 5000

It’s probably a better idea to no longer use port 5000 as that’s reserved for Airplay Receiver on macOS Monterey.

Just to run the server on a different port. There isn’t any need to turn off Airplay Receiver.

python3 -m http.server 4999

or

export FLASK_APP=hello
flask run -p 4999
Answered By: Nick

I had the same issue I upgraded the mac os from big sur to Monterey , i had a node API that runs on port 5000 which was now failing due to the fact that the API was trying to use the port which was being used with an operating feature which was airplay so as solution i used turn off air play feature then my app starting working.

Answered By: kelvin nyadzayo