48

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
noname
  • 511
  • 1
  • 3
  • 5

2 Answers2

137

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick
  • 1,386
  • 2
  • 7
  • 2
  • 2
    How attentive you spotted and explained the root-cause: a system-reserved port for Airplay ️ The `flask run -p ` solution was already suggested. Since question was not clearly raised, some research on "OSError: [Errno48]" and suggesting a link can do next time: [similar question](https://stackoverflow.com/a/51310430/5730279). – hc_dev Nov 18 '21 at 23:04
  • 1
    Thanks! This helped me. I'm just curious how did you come across this? – Parth Jan 05 '22 at 06:54
  • Apple should change this port. They can take a random high range port when 5000/7000 is in use so they can do this anyway. I guess this solution only works until reboot? Is there a way to change this default port low level (in a plist file or something) so it persists after reboot? – Koen Jan 19 '22 at 10:20
  • Any ideas how it is possible that I could use this port without problems in FF but was not working in Chrome (until I disabled Airplay Receiver, now it works in both) ? – Fygo Apr 12 '22 at 09:09
  • Is there also a way to disable it via console? – sunwarr10r May 09 '22 at 10:15
  • Also had the same question and found the same answer you give in this blogpost: https://nono.ma/port-5000-used-by-control-center-in-macos-controlce – hasan Oct 07 '22 at 09:37
  • I tried this ```export FLASK_APP=hello flask run -p 4999``` but after that last command I got an error ```Error: Could not import 'hello'.``` Not sure if it matters, but I'm in a virtual environment. – Yogesh Riyat Jan 29 '23 at 00:19
  • Same issue on Ventura. Fixed it for me. – MoD Feb 14 '23 at 09:47
  • Why on earth would they not just prevent Flask from trying to listen on that port? Surely that would be much easier to troubleshoot than letting Flask think it's connected, but then blocking all requests with no explanation. – Miguel Guthridge Mar 15 '23 at 08:34
1

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.