Flask server not visible from my public ip address

44,009

Solution 1

Do you have DHCP activated on your router? If yes do you see your host as 192.168.1.11 in there?

You have to use '0.0.0.0' on host, that tells Flask to listen on all addresses. Try specifying the port with quotes as app.run(host="0.0.0.0", port="33")

Solution 2

change it to app.run(host= '0.0.0.0', port="33") to run on your machines IP address.

Documented on the Flask site under "Externally Visible Server" on the Quickstart page: http://flask.pocoo.org/docs/0.10/quickstart/#a-minimal-application

Add port forwarding to port 33 in your router Port forwarding explained here http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/

Solution 3

You must give the public ip address/LAN ip address as an argument to app.run method. When you don't provide host argument, it works fine with http://localhost:8888/ and http://127.0.0.1:888/, but not to access outside the system where you are running the REST services

Following is the example. app.run(host="192.168.0.29",debug=True, port=8888)

Share:
44,009
Keatinge
Author by

Keatinge

Updated on November 12, 2020

Comments

  • Keatinge
    Keatinge over 3 years

    I'm trying to run a flask server on my desktop PC that is publicly available on the internet. I've done the following:

    I'm using the following code as a test webserver

    from flask import Flask, request, redirect
    app = Flask(__name__)
    
    @app.route("/")
    def hello_world():
        return "Test 123 "
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port="33")
    

    When I open my browser to: http://192.168.1.11:33/ the page displays properly, I see "Test 123"

    My problem comes when trying to connect to my webserver from my public ip address When I open my browser to http://xx.xxx.xxx.xx:30 (my ip address) all I see is "this site can't be reached, xx.xxx.xxx.xx refused to connect"

    I've looked up all the stack overflow answers, I've done the following:

    • Turned off windows firewall
    • Changed host from "192.168.1.11" to "0.0.0.0"
    • Tried a different port

    screenshot of code running and error shown: http://i.imgur.com/a05GvEs.png

    My question is: What do I need to do to make my flask server visible from my public ip address?

  • Keatinge
    Keatinge about 8 years
    I switched the port to "33", same problem. As for the DCHP, my host wasn't on there before, but I added it now. I still am experiencing the same problem. (This site can't be reached) Here's a screenshot of my router's DCHP page, maybe you can see some problem I don't. i.imgur.com/kkv07MI.png
  • Keatinge
    Keatinge about 8 years
    I have done both of these, just tested again right now. I still cannot connect. My port is 100% forwarded, tested online. i.imgur.com/uZISBqI.png
  • Keatinge
    Keatinge about 8 years
    From a mobile phone disconnected from wifi.
  • ganeshredcobra
    ganeshredcobra about 8 years
    @Racialz So you are using mobile data for internet connection and in browser you are trying public ip:33 are you trying this way?
  • 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.
  • zabop
    zabop over 2 years
    First link broken.