Introduction
As we know that Flask default port number is 5000. If you want to run two Flask server simultaneously or concurrently this will produce an error for the later website to run. This is due to the port can only have unidentical or unique port number from one to another.
To enable both website runs together we need to change the port number.
Change-Flask-Port-Number
Before we are going to change the port number for Flask website please note that avoid naming any of your Python file .py extension as flask.py. As this will be conflict with Flask itself.
In this tutorial I will guide you on how to change the Flask port number. You can choose either to change from the script itself or change using the flask command from the terminal.
Currently I have two Flask websites flask-red and another one is flask-blue. Let me show you for both websites.
$ tree -L 2
This will produce a tree for each website. Both had installed with Flask module using pip
└── flask-blue
│ ├── index.py
│ └── virtual
└── flask-red
├── main.py
└── virtual
I activated virtual environement for both flask-blue and flask-red by typing
$ source virtual/bin/activate
Below are main.py and index.py scripts.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return '''
<h1 style="color:red">Hello world! Welcome to flask-red</h1>
'''
if __name__ == '__main__':
app.run(debug=True, port=3300)
main.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return '''
<h1 style="color:blue">Hello world! Welcome to flask-blue</h1>
'''
if __name__ == '__main__':
app.run(debug=True)
index.py
Now, let’s run first flask-blue web server script index.py
$ python3 index.py
This will print a terminal status flask webserver.
* Serving Flask app 'index'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 434-356-643
Remember do not run this in production deployment!
Next, run another flask-red web server script main.py by typing into the terminal
$ python3 main.py
Once hit Enter it throw an error to the shell
* Serving Flask app 'main'
* Debug mode: on
Address already in use
Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.
This error a result of port 5000 has been used by flask-blue and still running.
Change-from-python-script
To change the port number simply add port=xxxx. xxxx is a 4 digit number that will look like this with main.py.
if __name__ == "__main__":
app.run(debug=True, port=3300)
index.py
As you can see the port number is set to 3300.
Again, now run it from the terminal.
$ python3 main.py
Now, let’s up the website and see if it’s still produce an error.
* Serving Flask app 'index'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:3300
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 434-356-643
Change-from-shell-using-flask-command
Another way of temporarily change for existing port number is by using flask command in the shell/terminal. This will override the existing port number if any has been set inside the python script like we had did just now.
However, please take note that to enable this the script that you would like to run as entry point for flask application should either name as wsgi.py or app.py.
Let me show you. Back to flask-red application and quit pressing CTLR+C.
Now, let’s run this command into the shell/terminal.
$ flask run -h localhost -p 8080
Once you pressed Enter it appears that flask-red fails to load. A notification tells that could not locate either wsgi or app to enable it loads as flask web application.
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
Once change the name from main.py to either wsgi.py and app.py it runs back perfectly. I am changing to wsgi.py as a demonstration to show it is working in my flask web server.
I am going to change using the shell/terminal
$ mv main.py wsgi.py
Now, let’s run again and change the port number from the terminal to 8080
$ flask run -h localhost -p 8080
Finally, it’s up and running as expected.