Flask Waitress Simple Webserver - python

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.

Related

Connect to Flask Server from other devices on same network

Dear smart people of stackoverflow,
I know this question has been asked a lot here but none of the posted solutions have worked for me as of yet. Any help here would be much appreciated:
The Problem: Cannot connect to flask app server from other devices (PCs, mobiles) on the same network. (in other words: localhost works perfectly but I cannot connect from external device)
What I've Tried:
1) Setting app.run(host='0.0.0.0', port=5000, debug=True, threaded=True) in the app.py so that the server will listen on all available network interfaces.
2) Enabling TCP traffic for port 5000 in local network in Windows Defender Firewall (with inbound and outbound rules added)
3) Using my host PC's IPv4 address in the URL bar of my external device's browser in the following format: http://<host_ipaddress>:<port>/
4) Using my host PC's hostname in the URL bar of my external device's browser in the following format: http://<host_name>:<port>/
5) Running the app.py file from Windows Powershell and Python (.py) Executor
None of these solutions has worked so far, even after attempting to connect from a few different external devices. Thanks in advance for your help!
I solved the issue by changing my home network profile to private instead of public, which allows my PC to be discoverable. Completely overlooked that!
Hope this helps someone!
Here is the method which worked for me.
Find the ip address of system using ifconfig command
Replace the host ip in code with your ip
server.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def about():
return 'It worked!'
if __name__ == '__main__':
app.run(host='192.168.43.81', port=5000, debug=True, threaded=False)
To run the program:
python3 server.py
I know it is late, but I believe it can help others.
Much depends on how you're running your app. From what you've written, I'm guessing that you have
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)
If that's not working, changes are good that you're using flask run to start things up. The problem here is that flask will import your application before looking for app in your application's namespace. The problem is that __name__ will then reflect the name of the base file, and not __main__, so app.run() never gets run.
If that's the case, try passing --host=0.0.0.0 as an argument.
If you are on windows go to your internet settings and change network profile from Public to private
I did a similar setup with my django project. Your PC network settings are probably all good, but your router ( at least for wlan) is blocking the traffic. I solved this by tampering with needed settings in router manager api, which can be found from 192.168.1.1 in your local network. You can check your device ip addresses etc.
Go to your wifi settings and change network profile from public to private.
If its already private click on configure firewall and security settings shown below which opens windows security
There disable the microsoft defender firewall for private connections.
This worked for me

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?

Can't access Flask app running on 0.0.0.0 on GCE

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 \

Porting domain to raspberry pi server

I'm running a Flask app installed on my Rasperry pi and I am able to access it via the local network at the internal IP and port 8080 (192.168.0.21:8080). I am now trying to access it externally from the network.
I have a netgear router that I've setup to forward port 8080 to 192.168.0.21 (my raspberry pi's internal IP). When I visit aaa.bb.cc.dd:8080 (my external IP) I don't see anything, what am I doing wrong here?
This is the line I am using to start the app from my raspberry pi:
app.run(host='192.168.0.21', port=8080, debug=True)
Ideally I would like to have a subdomain on a domain that I own point at this app, would I basically just point the A record for that subdomain at my external IP?
Update
I've since tried several different ports (ie. 8910, 87, 22) and forwarded these to my raspberry pi's internal IP still to no avail :/ I've even taken a step back and made a super simple hello world app and I am still not having any luck
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'hello world'
if '__name__' == '__main__':
app.run(host='0.0.0.0', port=8910, debug=True)
My router forwarding settings are below:
And then I am basically going to aa.bb.cc.dd:8910 (where aa.bb.cc.dd equals my external ip).
I've also tried porting port 8000 to 80 and then forwarding it to the pi and still nothing.
Solution
I ended up porting the external port 80 to internal port 8000 at the static IP address for my raspberry pi and then running the raspberry pi server on port 8000. I then forwarded my sub-domain to my external IP address and voila!

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