Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask server not visible from my public ip address

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

  • Set up a static IP address: 192.168.1.11 (http://i.imgur.com/Z9GEBYV.png)
  • Forwarded port 33 on my router to my static ip address (http://i.imgur.com/KGNQ2Qk.png)
  • Setup flask to use my static ip and port: 33

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?

like image 291
Keatinge Avatar asked Apr 13 '16 11:04

Keatinge


People also ask

What is the default IP address for flask?

Parameters of the flask run command. --host – the IP address of the web server to run your Flask application on. The default value is '127.0. 0.1'.

How do I change my IP address on flask?

Another thing you can do is use the flask executable to start your server, you can use flask run --host=0.0. 0.0 to change the default IP which is 127.0. 0.1 and open it up to non local connections.

How do I make my flask server public?

Today, there are two ways to expose your flask application to the internet. Deploy the web application in your office server which has a public IP address and domain name. Deploy the web application in the cloud such as AWS, MS Azure, GCP or web hosting companies like GoDaddy, SiteGround, A2Hosting etc.


2 Answers

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")

like image 147
Kreet. Avatar answered Oct 26 '22 23:10

Kreet.


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)

like image 21
Ramesh Seera Avatar answered Oct 26 '22 23:10

Ramesh Seera