How can one configure flask to be accessible via public IP interface?

11,001

run flask using

app.run(host="0.0.0.0:PORT#")

look for your IPv4 Address using ifconfig in your command prompt, and in your browser you can access your app using your ipaddress:port#

You might have to configure your router (port forwarding) to make it publicly available, otherwise you can download/use https://ngrok.com/ ----> This will expose your localhost behind a NaT or Firewall to the internet.

Once you download, you can configure it by running

ngrok http localhost:5000

this will provide you a RANDOMsubdomain.ngrok.io and you can test your app from pretty much anywhere

Share:
11,001
Dark Star1
Author by

Dark Star1

The true mind can weather all the lies and illusions without being lost. The true heart can toughen the poison of hatred without being harmed. Since beginning-less time, darkness thrives in the void, but always yields to purifying light.

Updated on June 18, 2022

Comments

  • Dark Star1
    Dark Star1 almost 2 years

    I am unable to resolve any methods externally via their url once I set the SERVER_NAME or host property in flask. The following is my app init config:

    flask_sqlalchemy import SQLAlchemy
    from flask.ext.cors import CORS
    __authors__ = 'DarkStar1'
    
    from flask import Flask
    
    app = Flask(__name__)
    app.config.from_object('config')
    app.config['DEBUG'] = True
    app.config['SERVER_NAME'] = '0.0.0.0'
    #app.run(host='0.0.0.0')
    CORS(app)
    
    db = SQLAlchemy(app)
    
    from oms import user_service, person_service
    

    I can set the DEBUG param/property but attempting to set the host or SERVER_NAME results in all urls, http://< hostname >:5000/test for example, resulting in 404s. Since the server is a remote dev server, I am able to tunnel and get 200s on all urls from flask via localhost. The port is enabled and the flask version I am working with is 0.10.1 on python 2.7.6. I have search and read docs to no avail.

  • Dark Star1
    Dark Star1 almost 8 years
    run.app() or app.run()?
  • Dark Star1
    Dark Star1 almost 8 years
    Got it up and running with app.run(host="0.0.0.0")
  • glls
    glls almost 8 years
    I had forgotten to add host and the quotes, thx
  • alexdlaird
    alexdlaird about 4 years
    Even simpler, use pyngrok (pip install pyngrok) to invoke and manage ngrok right from within your server.py. Here's a full Flask example, but basically you'd just need to from pyngrok import ngrok and then ngrok.connect(5000) when you're defining the routes.
  • glls
    glls about 4 years
    nice, did not know of this.