Home > Back-end >  localhost:5000 unavailable in macOS Monterey
localhost:5000 unavailable in macOS Monterey

Time:11-09

Cannot access a web-server on localhost port 5000 on macOS Monterey (Flask or any other).

E.g. use the built-in http server, 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 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
* 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

CodePudding user response:

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 Preferences -> Sharing -> Untick Airplay Receiver.

enter image description here

See more details

You should be able to re-run 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 run the server on a different port. No need to turn off Airplay Receiver.

$ python3 -m http.server 4999

or

$ export FLASK_APP=hello
$ flask run -p 4999
  • Related