Porting domain to raspberry pi server - python

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!

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.

Run flask file on wlan1 instead of wlan0

I have a raspberry pi, a flask server, a flask client, and two different networks.
when I connect a wifi adapter to the raspberry pi I can see that I have a new interface called "wlan1" is there a way to run a the server for example on "wlan0" and the client on "wlan1".
what I'm trying to do is run the server on a different network than the client (while both of them are on the pi).
Server:
For the server part, you need to "bind" the listening socket to the IP address of wlan0.
Find the IP address of wlan0 using ifconfig wlan0 or ip addr show dev wlan0 (e.g. 192.168.0.2)
Bind the Flask server to that IP address using app.run(host='192.168.0.2', port=80)
If you bind to 0.0.0.0, it will be reachable from all network devices.
Client:
A little bit more involved, take a look at how "routing tables" work for the theory.
Find out the IP address of the server that your client will connect to (e.g. 93.184.216.34)
Find out the IP address of the default gateway on the interface wlan1, for example with ip route (look for "default via dev wlan1"), e.g. "default via 192.168.1.1 dev wlan1"
Add a route to that IP address via the gateway and interface, using route add 93.184.216.34 gw 192.168.1.1 dev wlan1
Note that the routing table will affect all programs on the raspberry pi, not just your client application.

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?

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