Why Sweating the Details Matter: Flask, port 5000, localhost, Safari and macOS
Pop quiz : Suppose you are developing a Flask app. Flask uses port 5000 as its default port. Now suppose that you have your Flask app running on macOS on port 5000. You're testing in Safari 1 and you enter this URL into the address bar: http://localhost:5000 . Question : What will happen? Answer : You won't get your Flask app. Instead, you'll likely get a whole lot of nothing, which can be very…
Developing a Flask app involves running it on a specific port, typically port 5000, which can lead to confusion when testing in Safari on macOS. This is because port 5000 is also the default port for AirPlay, potentially causing conflicts. However, this is not the primary issue.
The real problem lies in how localhost resolves to an address. Contrary to popular belief, localhost does not necessarily resolve to 127.0.0.1. Instead, macOS prefers IPv6 and will resolve localhost to ::1, an IPv6 address. This means that when you enter http://localhost:5000 in Safari, you may not see your Flask app, as the server might be bound to ::1:5000, where AirPlay is running.
To access your Flask app, you should use http://127.0.0.1:5000. This works because Flask's development server binds to 127.0.0.1 (IPv4 loopback), not ::1. Thus, sweating the details of ports, addresses, and sockets is crucial to ensure your Flask app runs smoothly.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.