How to see if a Flask app is being run on localhost? How to see if a Flask app is being run on localhost? flask flask

How to see if a Flask app is being run on localhost?


You'll want to look at the configuration handling section of the docs, most specifically, the part on dev / production. To summarize here, what you want to do is:

  • Load a base configuration which you keep in source control with sensible defaults for things which need to have some value. Anything which needs a value should have the value set to what makes sense for production not for development.
  • Load an additional configuration from a path discovered via an environment variable that provides environment-specific settings (e. g. the database URL).

An example in code:

from __future__ import absolute_importsfrom flask import Flaskimport .config  # This is our default configurationapp = Flask(__name__)# First, set the default configurationapp.config.from_object(config)# Then, load the environment-specific informationapp.config.from_envvar("MYAPP_CONFIG_PATH")# Setup routes and then ...if __name__ == "__main__":    app.run()

See also: The docs for Flask.config


Here is one way of doing it. The key is comparing the current root url flask.request.url_root against a known url value you want to match.

Excerpt taken from github repo https://github.com/nueverest/vue_flask

from flask import Flask, requestdef is_production():    """ Determines if app is running on the production server or not.    Get Current URI.    Extract root location.    Compare root location against developer server value 127.0.0.1:5000.    :return: (bool) True if code is running on the production server, and False otherwise.    """    root_url = request.url_root    developer_url = 'http://127.0.0.1:5000/'    return root_url != developer_url