This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Configure Flask dev server to be visible across the network
(17 answers)
Access localhost from the internet [closed]
(6 answers)
Closed 2 years ago.
Hello guys so I wanted to be able to host an API on a raspberry and acces it from computers that are on another networks.
this is my simple code that's just for test as I need only to be able to acces it remote with the public ip
import flask
from flask import request
app = flask.Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return '123'
app.run(host='0.0.0.0', port=3138)
So I have created a port forward in my router option on the port 3138 linked with the static internal ip of the raspberry and I tried to acces it remotely like this: <public_ip>:3138/ it should show "123" but it shows nothing, it won't even load, do you have any ideeas how to be able to acces it in this way ?
Can you do some tests:
Have you tried to access it from your local network first (to make sure the port is open)?
Can also try run netcat on the raspberry(to exclude your program is not working) : "nc -l 3138" Then access the port from your mobile phone (should not be on connected to your network)
Setup your PC to use same ip and disconnect raspberry (to make sure port is open)
Check that you have a public IP so its not a Carrier-grade NAT (https://en.wikipedia.org/wiki/Carrier-grade_NAT). Check if your ip starts with 10.x.x.x, 172.16.x.x or 192.168.x.x (This can be indication its CGNAT)
It's not advisable to you the flask development server in production. I'll advise you use on of the WSGI suited for production(you may use waitress):
1. pip install waitress
2. create a file server.py (or whatever name suites you)
#content of server.py
from waitress import serve
import main #import flask app main file
serve(main.app, host='0.0.0.0', port=8080)
3. run server.py
4. access you app via:
http://<public_ip>:8080
Related
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.
I own a domain name, how can I get my flask app to run on it, using my public IP address (assigned to me by my internet provider)? I don't want it to run on localhost, or my private IPv4 address, I want it to run on my Public IP address. Thanks in advance.
If it helps, I am using Google Domains.
Do I need to add anything to my flask app?
I'm currently running it like this:
if __name__ == "__main__":
db.create_all()
app.run(debug=True, host="0.0.0.0", port=80)
Was this a public IP provided by your Internet provider? Or is it a public IP provided by a web hosting company? The two answers are different.
If it came from a web hosting company, then you need to upload your flask app to your account on their server. Note, however, that most shared hosting companies will not let you run an app 24/7. They want to support FastCGI servers that come and go.
If this was provided by your Internet provider, then you will need to provide and configure a server. You will need to have your router redirect port 80 and 443 to that server. You will then need to run your flask app on that server. Make sure your server is secure, because public-facing IP address get hammered by scammers.
This question already has answers here:
What is going on when I set app.wsgi_app = ProxyFix(app.wsgi_app) when running a Flask app on gunicorn?
(2 answers)
X-Forwarded-Proto and Flask
(1 answer)
Closed 2 years ago.
I am trying to capture client ip in logs and getting correct ip on development server but after deployment on sit server, getting wrong ip by default (10.46.0.0). Please someone suggest, what else can be used in flask python? Thanks in advance.
Code:-
Import request
Ip = request.environ['REMOTE_ADDR']
The IP address you retrieve is part of the CIDR block 10.0.0.0/8 and reserved for private networks. This tells me that your application deployed behind a reverse proxy which performs requests to your application on the original users' behalves. A reverse proxy would typically inform the upstream service (your application) about the originating IP address by adding the header X-Forwarded-For to the proxied request. This depends on the configuration of the reverse proxy, so you should contact the people in charge of the deployment server about its configuration.
It seems like you got the address from a Reverse Proxy. The original client IP may be forwarded as part of the proxy chain.
This worked for me.
client_ip = request.headers.get(
'X-Forwarded-For',
request.headers.get('Client-Ip',
request.remote_addr))
There is a difference between WSGI and CGI.
WSGI: request.headers
CGI: request.environ
This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
How to access Java servlet running on my PC from outside?
(3 answers)
Remote Desktop From Outside Network
(2 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
So i realize this might have something to do with me not understanding how local IPs work, but lets say my computer is running on the public ip: xxx.xxx.xx.xx
And I have this flask code:
from flask import Flask, request
app = Flask(__name__)
#app.route('/testingdata', methods=['POST'])
def result():
print(request)
print(request.form['DevEUI_uplink'])
return 'Received !'
if __name__ == '__main__':
app.run(host = '0.0.0.0', debug=True, port=5000)
This lora plataform is able to send POST data to an URL which I managed to do so to Beeceptor whereas the data came out like this:
{"DevEUI_uplink": {"Time": "2018-10-19T13:15:17.531+00:00","DevEUI": "xxxxxxxxxxxxxxxx","FPort": 18,"FCntUp": 4,"ADRbit": 1,"MType": 4,"FCntDn": 4,"payload_hex": "7b2274656d7065726174757265223a32337d",...}}
But when I try to point the URL to xxx.xxx.xx.xx:5000/testingdata, nothing gets there.
I welcome any newbie friendly knowledge on this since I've been on and off at this for a while.
Edit 1: Thank you for the hosting tip, still doesn't work tho.
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().