Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask on Google Compute Engine - cant reach from outside/browser

I've setup a simple Flask server on my Compute Engine, which runs internally on 127.0.0.1:5000.

If i ssh onto that instance and curl 127.0.0.1:5000 i see the hello world message and the Flask instance shows a get in its logs.

However, if i use the external ip address through a browser it says the site doesn't exist.

I've tried to setup multiple diff firewalls, pointing to either 0.0.0.0/0 or 127.0.0.1/0 and including both tcp:5000 and udp, or just tcp or just saying allow all ports.

Nothing works.

I did try running netstat -plant and it said that port 5000 was attached to python3 (which is my Flask instance) but that the port was closed.

UPDATE:

Got it working, but will just add some more logging/output in case anyone else comes to this issue also.

me@instance-1:~$ nmap 1.2.3.4 Starting Nmap 7.40 ( https://nmap.org ) at 2018-08-19 15:30 UTC Nmap scan report for 4.3.2.1.bc.googleusercontent.com (1.2.3.4) Host is up (0.0016s latency). Not shown: 995 filtered ports PORT STATE SERVICE 22/tcp open ssh 80/tcp closed http 443/tcp closed https 3389/tcp closed ms-wbt-server 5000/tcp closed upnp

me@instance-1:~$ netstat -plant (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 29414/python3 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 1 10.142.0.2:22 118.24.87.43:51760 LAST_ACK - tcp 0 0 10.142.0.2:41788 169.254.169.254:80 CLOSE_WAIT - tcp 0 0 10.142.0.2:41796 169.254.169.254:80 ESTABLISHED - tcp 0 0 127.0.0.1:55258 127.0.0.1:5000 TIME_WAIT - tcp 0 0 10.142.0.2:41794 169.254.169.254:80 ESTABLISHED - tcp 0 0 10.142.0.2:22 74.125.73.96:47952 ESTABLISHED - tcp 0 816 10.142.0.2:22 74.125.73.36:51834 ESTABLISHED - tcp 0 0 10.142.0.2:41792 169.254.169.254:80 ESTABLISHED - tcp6 0 0 :::22 :::* LISTEN -

Then I restarted flask using

flask run --host=0.0.0.0 --port=5000

and now...

me@instance-1:~$ netstat -plant (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 31365/python3 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 10.142.0.2:45802 169.254.169.254:80 ESTABLISHED - tcp 0 0 127.0.0.1:55258 127.0.0.1:5000 TIME_WAIT - tcp 0 0 10.142.0.2:41794 169.254.169.254:80 CLOSE_WAIT - tcp 0 0 10.142.0.2:45804 169.254.169.254:80 ESTABLISHED - tcp 0 0 10.142.0.2:22 74.125.73.96:47952 ESTABLISHED - tcp 0 0 10.142.0.2:45800 169.254.169.254:80 ESTABLISHED - tcp 0 816 10.142.0.2:22 74.125.73.36:51834 ESTABLISHED - tcp6 0 0 :::22 :::* LISTEN - me@instance-1:~$ nmap 1.3.3.4 Starting Nmap 7.40 ( https://nmap.org ) at 2018-08-19 19:24 UTC Nmap scan report for 4.3.2.1.bc.googleusercontent.com (1.2.3.4) Host is up (0.0012s latency). Not shown: 995 filtered ports PORT STATE SERVICE 22/tcp open ssh 80/tcp closed http 443/tcp closed https 3389/tcp closed ms-wbt-server 5000/tcp open upnp Nmap done: 1 IP address (1 host up) scanned in 4.75 seconds

like image 911
Russ Wheeler Avatar asked Aug 19 '18 16:08

Russ Wheeler


1 Answers

i assume that you are using default run method, in that provide host address as 0.0.0.0 and it will do the trick

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hi'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

This will allow you to access your website out of local-host, with ip address.

if you are using flask run method then run it as

flask run --host=0.0.0.0 --port=5000
like image 132
D. S Avatar answered Oct 22 '22 05:10

D. S