Can't access Flask app running on 0.0.0.0 on GCE - python

I set Firewall Rule for my local google compute instance at host '0.0.0.0' and port 7000.
And I executed python server.py, it was running on https://0.0.0.0:7000
but when I enter https://external-ip:7000 on my local browser it did not work.
So how can I run flask on google compute engine and open in my local computer browser?
server.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run(debug=1,port=7000,host='0.0.0.0')

A few things:
Check your VPC firewall:
https://cloud.google.com/vpc/docs/firewalls
In your terminal, see if connections are working locally on that host by issuing:
telnet localhost 7000
If it connects then it's either firewall or the below.
If you're running on https, you'll probably need to have something along the lines of:
context = ('host.crt', 'host.key')
app.run(host='0.0.0.0',port='7000', ssl_context=context)
Lastly, it's https:// not \

Related

Flask Waitress Simple Webserver

I am trying to set up a simple webserver. This is the simplified code.
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route("/data")
def get_data():
return {"abc"[i]:i for i in range(3)}
if __name__=="__main__":
# app.run(debug=True, host="0.0.0.0", port=8080)
serve(app, host="0.0.0.0", port=8080)
I can connect to this on my desktop and my phone (which is on my WiFi) and it works as desired.
The issue comes when a connection is attempted from a device not on my network (say a phone on a different network). The response is ERR_ADDRESS_UNREACHABLE.
I setup an inbound rule in my firewall settings. I'm on a windows 10 OS right now if that matters.
ProtocolType=TCP (it was default)
LocalPort="Specific Ports" and 8080
RemotePort="All Ports"
I also read I should set up port forwarding in my router so I followed these instructions from my ISP.
It's in the Service List
Global Port Range is 8080-8080
Protocol is TCP/UDP (again, default)
Host Port is 8080
I'm not sure what else I should change.
Thanks!
Answering my own question for posterity.
My issue was some AT&T weirdness. I turned on IP passthrough in my router settings and other people can connect to the server.
If I run ipconfig in my cmd prompt, I get an IPv4 of A.B.C.D, but https://whatismyipaddress.com/ responds with E.F.G.H.
I can connect to A.B.C.D:8080/data from my computer but not the other IP.
Someone not on my IP can connect to E.F.G.H:8080/data from a different network but not the other one.
The final takeaway is that I should probably just use some sort of ns services like cloudflare.

FLASK: curl: (7) Failed to connect to 127.0.0.1 port 5500: Connection refused

I am trying to transition from making Rshiny apps to flask-react apps. Its definitely a steep learning curve so far!
I am sort of following a few tutorials e.g (https://dev.to/arnu515/build-a-fullstack-twitter-clone-using-flask-and-react-1j72) to try and get some basic functionality down .
However some reason curl can't seem to interact with my app. I've tried putting the urls with and without quotes but get the same response. Also I tried the default 5000 port as well. I am running the app in windows:
C:\Users\Marc\flaskTest\backend>curl "http://127.0.0.1:5500"
curl: (7) Failed to connect to 127.0.0.1 port 5500: Connection refused
app.py code
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
#app.route('/')
def index():
return "hello"
#app.route('/message', methods=["GET"])
def message():
message ="my message"
return jsonify({"message": message})
if __name__ == "__main__":
app.run(debug=True, port=5500)
You used jsonify in the view function but haven't imported it before, so there would be error when Flask app runs.
Actually you can just write code like return {"message": message}, it would do the same thing with jsonify does if you are using latest version of flask.
Try:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5500)
Also in windows cmd type ipconfig IPV4 address. Suppose your IPV4 address is 192.168.X.X, access the website as http://192.168.X.X:5500.
Read what it does: Externally Visible Server

Attempting to set up two python flask servers, no port other than 80 will function

I'm doing this as a temporary measure, eventually both servers will be in different locations. For now, I am trying to host one of the servers on a raspberry pi at 192.168.1.123, port 80, and the other on my computer (temporarily) at 192.168.1.125, port 8080.
I have enabled port forwarding on my router, and allowed the ports in my firewall shown here.
I have also contacted my ISP, and they have told me they are not blocking any ports.
This is the code I am using for both, just changing the port=8080 to port=80.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def landing_page():
return "Hello World"
if __name__ == '__main__':
app.run(debug=False, port=8080, host='0.0.0.0')
Where am I going wrong?

python server http://0.0.0.0:8080/

I am having a problem using webpy. Here is my code
import web
urls = ('/(.*)', 'hello')
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
When I run it, it starts on http://0.0.0.0:8080/ and it says I don't have access to localhost, which is 127.0.0.1. How can I change it? I tried python ./server.py but this didn't work
When web.py starts up, by default it listens on all IPv4 interfaces, on port 8080. It says this by reporting http://0.0.0.0:8080/.
In this context, 0.0.0.0 isn't a real address, so you can't point your browser to that address. You can point your browser to either the "real" address (you may have more than one for the same computer), or to the loopback address 127.0.0.1, or localhost. Loopback only works locally (i.e., your server is on the same computer as your browser/client.)
You can change web.py's default:
host$ python app.py
http://0.0.0.0:8080/
^C
host$ python app.py 127.0.0.1
http://127.0.0.1:8080/
^C
host$ python app.py 127.0.0.1:9999
http://127.0.0.1:9999
^C
Had the same problem and as #pbuck pointed out you need to run it probably on a loopback address like the localhost:
http://localhost:8080

Problems with external visibility of Flask web-server

I have managed to to install flask and run the hello-world script:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
I was impressed how easy it was. Then I wanted to make my web-server visible externally. As recommended, I put host='0.0.0.0' as the argument of run function. Then I found my IP address (in Google) and put it in the address line of my web browser (while the hello-world-script was running).
As a result I got: "A username and password are being requested" and a dialogue box where I need to put a user name and password. I am not sure but I think it comes from my wireless network. Is there a way to change this behaviour?
How are you trying to run your application? If you run flask as app.run() - flask creates its own WSGI server on your host (by default 127.0.0.1) and port (by default 5000) (need permissions if port < 1000). If you run flask using nginx + WSGI or etc. your server resolves host and port.
Now it looks like you want get application by port which resolved your server like nginx or Apache. Try to get flask application by http://your-server-host-or-ip:5000 with the default port or try to change the port (set explicit) like app.run('0.0.0.0', 8080) and get it by http://your-server-host-or-ip:8080.
By the way, you can always get IP address using command-line tools e.g. ifconfig for Unix-like systems, or ipconfig /all for Windows.
To elaborate a little bit onto what #tbicr said, that password prompt indicates that you're trying to connect to your IP on port 80, which is most likely hosting an administration page for your router/modem. You want to connect to your IP on port 5000, the default port for Flask apps run with app.run().

Categories

Resources