Flask SocketIO How To Setup Production Server? [duplicate] - python

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
I currently have a project which uses Flask-SocketIO as the backend and a separate client that interacts with the Flask-SocketIO server. Everything is working when I run the server on my local machine. However, when I run the Flask-SocketIO server on a remote server via SSH and try connecting to the host IP address, the connection doesn't seem to work.
The flask-socketio server seems to run fine on the remote server machine as I can see things being printed into the console (running some machine learning models with tensorflow), but I just don't seem to be able to connect to it from the client.
So my main questions are:
What could be the reason why I'm unable to connect to the server remotely?
How exactly can I set up load balancing with nginx? I have looked at the documentation (https://flask-socketio.readthedocs.io/en/latest/#deployment) but I'm still unsure as to where to start.
Currently the project is configured to use the default server, using
if __name__ == '__main__':
socketio.run(app, port=5000)
I connect to it using
const socket = io('http://[ip address]:5000');
It works when I do
const socket = io('http://localhost:5000');
My server is hosted with DigitalOcean running Ubuntu
Any help would be greatly appreciated!

On production it is recommended to use a proper application server/reverse proxy combo such as Gunicorn or uWSGI as described in the documentation, but to answer your question, you should have passed the host argument to the .run() method as it defaults to 127.0.0.1:
https://flask-socketio.readthedocs.io/en/latest/#flask_socketio.SocketIO.run
host – The hostname or IP address for the server to listen on. Defaults to 127.0.0.1.
Which means that it will only listen on the local loopback interface and not the Internet interface. You should change it to the public IP address of the VPS, or to 0.0.0.0 to listen on all interfaces. For example:
socketio.run(app, host='0.0.0.0', port=5000)

Related

404 in python flask application on EC2

i'm kinda new to all this, so bare with me
I'm trying to deploy my flask app to EC2, and before getting to set a WSGI server, id like to try to connect to port 8080
the code can be found here: https://github.com/Klasyer/POBAP
I've set the flask app host to 0.0.0.0 and port to 8080
In EC2 i opened the inbound rule for 8080
When i try to open the app through the web (http://18.223.32.186:8080/) i get a 404.
What am i missing/doing wrong?
Thanks ahead for any help
Try attaching an elastic IP address to the instance that your running and once it is done you can access the flask app running in the instance. But you should include the port number always while accessing it.
If you need the app to be accessed without the use of port number then in your flask app change the port number to 443 which is for HTTPS and 80 for HTTP. If you need HTTPS the process is somewhat different because you need to make a purchase but for HTTP it's free. So go on with HTTP by using the port number as 80
app.run(host='0.0.0.0', port=80)

Flask Server for Listening to HTTP Responses

I am working on making a Flask server that can listen to a node that would like to contact the server at any point of time and then the flask server process the response. I would like to ask whether there's any method to listen to the connection attempted by the remote node.
Flask has a development server, documented here:
FLASK_APP=hello.py flask run
Note that the server is not intended for production. If you're looking for production server, you can read this page. There are several solutions, such as Gunicorn, uWSGI and more.

Flask server works on internal network but doesn't connect with VPN

SSH works everywhere
I recently obtained a static IP at work and attached it to an internal domain. I am able to SSH into the machine with ssh MY_DOMAIN regardless of whether I'm
physically at work and on the same network, or
connected with full-tunnel VPN.
Flask works at work
If I'm physically at work and start a Flask server (code below) after logging into the desktop the domain is routed to, I can open up MY_DOMAIN in a browser on any computer at work (no VPN) and see Hello There!.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80) # 127.0.0.1 didn't work either
# changing host to MY_DOMAIN or IP and port to 5000, etc. also didn't work
If I'm away from work but on VPN and SSH into the machine to start the Flask server, my connection times out when navigating to MY_DOMAIN in a browser.
Ping also works at work
On VPN, when I ping MY_DOMAIN, I see 0 packets received (all packets received when I'm at work):
9 packets transmitted, 0 packets received, 100.0% packet loss
If I turn off VPN away from work and ping MY_DOMAIN, I see:
ping: cannot resolve MY_DOMAIN: Unknown host
How am I able to SSH without any issues when I'm at work or away and on VPN, but can't interact with my domain in any other way on VPN?
I suspect this could be related to ports but I have limited networking knowledge.
And yep, I've done my research. This was the closest question on here connecting to flask app over VPN but it doesn't help (at least I don't think I want to deploy anything). I don't know if this is possible, but I think I want to configure: "forward incoming requests on port 80 of MY_DOMAIN to Flask app on localhost, port 80". (Edit: don't know any more after the Apache Web server works.)
Edit: I can connect to an Apache server on VPN but not Flask
I can navigate to MY_DOMAIN in a browser connected VPN and see "It Works" when I SSH and run:
sudo apachectl start
sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled -bool false
I wonder why this doesn't work with Flask.
(reference: https://www.quora.com/How-can-I-make-my-iMac-the-hosting-for-my-website)

How to access ports on a local server via ssh? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
I am running a flask server on my remote machine on the same network, whose IP is 192.168.1.11. Hence the flask server endpoint for the networks should be 192.168.1.11:5000 where 5000 is the default port chosen by flask.
However, when I hit a request from my local machine (IP: 192.168.1.10) to 192.168.1.11:5000 it doesn't return anything.
However when I run the following on my local machine (IP: 192.168.1.10) ssh -L 4000:127.0.0.1:5000 user#192.168.1.11 and then hit localhost:4000, it works. I understand that this is Local Port Forwarding in ssh.
But when I run jupyter notebook on the remote machine, it runs on the 192.168.1.11:8890. Then when I run 192.168.1.11:8890 from my local machine, it works.
Why does jupyter work and not flask via the default configs? Is it because jupyter does some sort of remote ssh port forwarding. How do I run some code i.e. that is some configs, etc like ssh -R xxxx:xxxx:xxxx xxxx.
It seems that your flask server listens only on loopback interface.
From docs:
Externally Visible Server
If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.
If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:
flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.

Heroku, flask, and python sockets?

I have a python script that runs on my computer. It opens a socket and prints anything it receives. This definitely works -- I've managed to connect to it from other computers and send it data.
The problem is that my heroku app fails to connect to the socket.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((daemon_socket_vars['host'], daemon_socket_vars['port']))
s.send("Hi!")
s.close()
The heroku app fails on the second line after timing out. When I run something identical on either my laptop, or a friend's laptop (while the python script that's acting as the server is running on my laptop in both cases) it works. Does anyone know why heroku would have problems with this? Thanks!
When running on Heroku, your server should bind to port specified in the environment variable PORT (say 7880, just for the sake of this discussion). It is not guaranteed to be 80, 5000, 8000, 8080, or anything else.
To the outside world, however, this will appear as port 80 or port 443. That is, if connecting from outside of Heroku, your client will be connecting to port 80.
One final caveat: when connecting from outside Heroku, your client will go thru the "Heroku Routing Mesh", which among other things does the 80-->something port "translation". The thing is, the routing mesh is an HTTP routing mesh: it will only accept incoming HTTP requests, and will route them (after sometimes altering them, like adding headers etc.) to your dyno.
So you can't just write a plain-sockets app on the Heroku and connect to it directly, you'll have to use HTTP as your transport.

Categories

Resources