21

I have one flask application script as given below :

from flask import Flask
app = Flask(__name__)

@app.route("/<string:job_id>")
def main(job_id):
    return "Welcome!. This is Flask Test Part 1"

if __name__ == "__main__":
    job_id = 1234
    app.run(host= '0.0.0.0')

I have another flask application script as below :

from flask import Flask
app = Flask(__name__)

@app.route("/<string:ID>")
def main(ID):
    return "Welcome!. This is Flask Test Part 2"

if __name__ == "__main__":
    ID = 5678
    app.run(host= '0.0.0.0')

The only difference between both the scripts is the argument name and its value. Now my question is assume that I am executing the first script. So I will get something like

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

When I execute http://127.0.0.1:5000/1234 in my browser I am able to see

"Welcome!. This is Flask Test Part 1"

Now with this server active, I am executing the second script. So again I get

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

But when I execute http://127.0.0.1:5000/5678 in my browser I am able to see

"Welcome!. This is Flask Test Part 1"

instead of

"Welcome!. This is Flask Test Part 2"

I don't understand where I am doing mistake. Any inputs or alterations will be helpful

5
  • 2
    It shouldn't work as both will run in the same port and the second one should complain. You have to add a port argument to the app.run() call using two distinct ones. Jan 11, 2018 at 11:10
  • Thanks @IgnacioVergaraKausel but on a single day my script needs to be executed with 'n' number of arguments. As a standalone python script it is working fine. But any idea on how to achieve the same when we need to expose the python script as a web service ?
    – JKC
    Jan 11, 2018 at 11:11
  • It's not clear to me what are you further asking, which is not part of your original question. Jan 11, 2018 at 11:19
  • sorry @IgnacioVergaraKausel My original question is just a simplified question for better understanding. But if you see the title of the post you can understand my real requirement
    – JKC
    Jan 11, 2018 at 11:28
  • You could start the two applications on diffrent ports (just add port = 5001 to the app.run call to start the app at port 5001 instead of 5000) Jan 11, 2018 at 12:07

3 Answers 3

34

Flask development server by default listens on port 5000 so when you run a Flask app without port number it will run on 5000.

You can run a number of Flask app on the same machine but with the different port numbers. Let's say your scripts names are script1.py and script2.py:

$ export FLASK_APP=script1.py
$ flask run --host 0.0.0.0 --port 5000

Open up a new terminal

$ export FLASK_APP=script2.py
$ flask run --host 0.0.0.0 --port 5001
2
  • 4
    Is there any problem with having multiple instances of flask open on different ports like this? As long as my computer can handle it, there aren't any other issues? Jun 25, 2019 at 14:48
  • @rvictordelta and anyone else that may be wondering. This is a normal way of separating two flask applications. You specify a port that is not running another application, as only one per port should be used. This will cause no issues as long as you don't overload your computer with servers. Feb 11, 2022 at 19:42
3

I have very simple solution for this problem just add this below line in your code app.run(port=3000)

simply assume that your first application is working using 5000 port and you try to execute another flask app it may using the same port which is 5000 so that's why you may face these problem so just simply add this line of code in which you can change your port number to execute the application on another port.

example:

if __name__ == '__main__':
    app.run(port=3000)

Note: instead of port 3000 you can enter any of your choice

1
2

Although other answer suggests to run both flask servers with different --port arguments, I wanted to share another issue to consider:

Clear the browser cache

Did you clear cache memory before running the second script?

There are times when browser has previous data of the URL (including first port) still stored. So it will display old data (from first script and port) instead of fetching new data (from second script and port).

You can clear the cache memory and then run the second script, access in browser and see it fetches the data correctly from the second app's URL.

1
  • thank you for your inputs. but already this is fixed and an answer has been accepted
    – JKC
    Sep 17, 2019 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.