Flask - How to make an app externally visible through a router?
Asked Answered
Q

1

21

The question in short

How do you run a simple Flask-based website visible to the internet, from a host PC which is accessing the internet from a wireless router?

Question details

I would like to make a flask application visible to the internet, as per Quickstart Guide.

If I launch the simple Flask app below, it becomes accessible from a computer on the same network as the host pc, but not from a device connected through the internet through another network.

The problem is similar to the one discussed here and here, with the added element that running from a home pc seems to suggest that external connections point to the xx port of the router, not to the xx port of the host pc, as is suggested in the comments in this post.

What I did

Referencing the code below, here's what I did:

  • Checked my IP address in Control Panel
  • disabled all network protection in the antivirus
  • run `ipconfig /all', being on a windows machine
  • finally opened a browser in a device connected to another network and pointed it to the appropriate IP:port address

The result is that "The webpage is not available".

Has anybody encountered the same problem? Is this a router issue?

Reference Flask app

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host= '0.0.0.0', port=9000, debug=False)
Quintie answered 14/5, 2015 at 15:31 Comment(6)
Routers are specifically made to prevent this kind of behavior and keep your home connection protected from the outside world. You might be able to find a workaround, but you shouldn't.Niu
Need to set up the router to forward requests to port 9000 of the IP of the computer that is running the flask appMeill
Why would you want to do this? I would recommend setting up a site on Heroku for something as trivial as this.Aerography
Probably just playing around, nothing wrong with learning the hard way :)Meill
If you're interested in learning then learning how to deploy Heroku or AWS will likely be of more use than opening up your home PC to the internet.Aerography
Thanks for the comments. To provide some context, these are initial prototyping experiments in a move from desktop-based GUIs (mainly PyQt) to web-apps. So yes, need to learn how to deploy Heroku. Home computer was an easy environment for a number of reasons (mainly local db access, and no prior knowledge of cloud-based services)Quintie
P
18

The basic setup will be to create a rule which will forward request to port 80 and/or port 443 to a destined host in your local network.

Example create NAT(address translation) and port forwarding rule to forward inbound HTTP/S requests to your local network host running your python application.

For example:

app.run(host= '192.168.0.58', port=9000, debug=False)

Your NAT rule should target 192.168.0.58 on port 9000.

Papillote answered 14/5, 2015 at 15:39 Comment(3)
Thanks! Next to figure out how to setup a router.Quintie
how to retrieve host ip address 192.168.0.58 pythonically ? is it possible ?Sauveur
#167006 There are a few approaches here - the accepted answer probably won't work on linux, so have a look at others.Compensate

© 2022 - 2024 — McMap. All rights reserved.