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 Safari1 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 confusing because of two overlapping factors.
It turns out, 5000 is the default port for AirPlay. You might think that Flask and AirPlay would conflict because they're on the same port. But that's not the case because a socket is more than a port. For a listening server it is an address family, an IP address, and a port — so 127.0.0.1:5000 (IPv4) and [::1]:5000 (IPv6) are two different sockets, even though the port number is the same.2
Now that we cleared that up we have to answer why localhost doesn't give us our Flask app. The answer, as you may have guessed at this point, is that localhost, contrary to popular belief, is NOT and address. It has to resolve to something: we're just used to it resolving to 127.0.0.1.
And therein lies the rub as the bard would tell us. localhost doesn't have to resolve to 127.0.0.1. It can just as easily resolve to ::1 which is an IPv6 address.
In fact, that's exactly what happens. macOS prefers IPv6 in an attempt to be more modern. It just so happens to be the case that on ::1:5000 AirPlay is running.
But if we put 127.0.0.1:5000 into the address bar, we get our Flask app because Flask's development server binds to 127.0.0.1 by default (e.g. IPv4 loopback) and not to ::1.
If you found this post useful, consider supporting my Open Source software work directly by donating directly at https://www.paypal.com/paypalme/realprofessortom
-
Or, like me, you stubbornly have Safari set as your default browser even though you also keep Brave and Opera open all the time for various reasons. ↩
-
IPv4 and IPv6 are two different network-layer protocols. ↩
They have different packet formats, different address sizes, and they are not directly interchangeable.
However, they are two versions of the same protocol idea (“Internet Protocol”), not two unrelated protocols like TCP vs UDP.
Top comments (0)