1

Hi All I am new to Python Flask. After reading basic manuals I tried to create a sample Flask application and successfully did in my machine.

My code (taken from various tutorials) is given below:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
    return "Welcome!"

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

As you can understand, this application prints "Welcome!" whenever I hit http://127.0.0.1:5000/. But when I try to access the application from some other machine within same network by replacing 127.0.0.1 with my system's IP address, I am getting "Network Error (tcp_error) Connection Refused" error

Any inputs on how to access Flask application (created in one machine) from another machine will be helpful

JKC
  • 2,498
  • 6
  • 30
  • 56

1 Answers1

14

Finally after hitting several other tutorials, I am able to access my application from remote machines as well.

The problem is if we just run the Flask app by "app.run()" command then it will listen only to the localhost / 127.0.0.1 . If we make the app listen everywhere by running it using "app.run(host= '0.0.0.0')" then I am able to access it even from other machines

Hope others also will get benefited with this.

Reference : http://dixu.me/2015/10/26/How_to_Allow_Remote_Connections_to_Flask_Web_Service/

JKC
  • 2,498
  • 6
  • 30
  • 56
  • Even simpler, use `pyngrok` (`pip install pyngrok`) to invoke and manage `ngrok` right from within your `server.py`. [Here's a full Flask example](https://pyngrok.readthedocs.io/en/latest/integrations.html#flask), but basically you'd just need to `from pyngrok import ngrok` and then `ngrok.connect(5000)` when you're defining the routes. – alexdlaird May 07 '20 at 00:21