Flask Web Server Port Mapping

Julian, Sat 01 July 2017, Flask

apps, beginner, Flask, learning, ports, python

If like me you’re obsessed with Python Flask, you might have asked yourself, “How on Earth do I run multiple Flask apps at the same time?!”.

It’s actually pretty simple!


What Happens at Default Settings

Before I get to the solution, I’ll first show you what happens if you leave everything at the default settings as it’s important to know.

For this test I’m running my Pay Calculator App and my Timezone List App together.

The app you launch first will always take priority. In this case, my Pay Calculator interface shows up on 127.0.0.1:5000. The interesting thing is that when I run the Timezone App, there’s no error. Python still launches a web server on 127.0.0.1:5000.

The catch is that all calls from my browser to localhost (127.0.0.1) are routed to the web server created by the Pay Calc app. If I try and browse to a web page that is unique to the Timezone App, I get a 404 error. The page doesn’t exist in the Pay Calc app and therefore the call fails.

As expected, the second I CTRL+C my Pay Calc app, everything springs to life for the Timezone app. Browsing to localhost brings up the Timezone interface and browsing to the aforementioned unique page works.


Specify a Port!

The solution? Specify a port number!

In Flask code, it’s the app.run() code that kicks everything off. Without that code, there’s no app.

By default, this starts the web server on 127.0.0.1:5000. We can change this!

if __name__ == "__main__":
    app.run(port=5001)

Believe it or not, it’s as simple as that!

Throw the port number you want to access the web app from to app.run() and the web server launches on that port. So simple and easy!


Conclusion and Discussion

This is as simple as it gets. There is however something else to discuss.

If you’re trying to run two or more concurrent web apps, it’s likely that you want these apps running in a sort of “production” environment. That is, you want them running all the time, it’s no longer just for a test.

That’s exactly my case. I want a few Flask apps running from my NAS on my local network at home.

The web server bundled in Flask is a development server. It may be fine for my home network but best practice mandates I use a dedicated web server like nginx.

Or another question, should I even use Flask for making production apps? Once I get to this level of production should I be moving to Django?

I’m actually not too sure! I’m definitely keen to hear everyone’s opinion on this. What do you use (if at all) for this sort of thing?

Do you use Flask for small apps and testing and Django for the bigger and badder stuff?

And as always, Keep Calm and Code in Python!

-- Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book