Start your Mac. Take the base Flask app from the quickstart page, and change the port to 6000, which gives you the following:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(port=6000)
Save this in a file named e.g. test.py
. Then create a virtualenv, run pip install flask
, and call test.py
. Here is what you will see on the terminal:
* Running on http://0.0.0.0:6000/ (Press CTRL+C to quit)
* Restarting with stat
So Flask claims to have bound to port 6000. Now start a browser and navigate to localhost:6000
. I was expecting to see the silly message Hello World
, which is the case when I leave out the port
argument to run
, and navigate to localhost:5000
. But here is what I see instead:
Now do Ctrl-C
on the terminal, and stop the running process. Change the port to 6001, rerun the command. Hello World
is back! How can this be? There are no other processes connecting to port 6000; lsof -i | grep 6000
returns 0 results, and if there were any processes, Flask would fail to bind to that port in the first place. Firewall is turned off.
Any ideas?
OK, found the answer. Browsers block certain ports, although they are not in the system port range, some of them in the ranges widely used for local web development. The links in this answer point to the rationale from the browser vendors and exhaustive lists. As the germans say, "Wieder was gelernt".
Thx to @glyphobet for his comment that led to the right answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With