Heroku, flask, and python sockets? - python

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.

Related

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)

Flask SocketIO How To Setup Production Server? [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 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)

How to run localhost on a port on a website with Flask

so I'm trying to host my flask app on a site only when it's accessed via a certain port for example. If a user goes to www.example.com on port 80 the site acts normal and they connect to the site. However if they visit www.example.com on port 5000 they are greeted with my flask app, how can I achieve this? Side note as people are getting confused, I don't own www.example.com I just want it to display what's running on localhost (flask) when it's accessed on a certain port.
You can run both the applications at the same time. Since Flask and the webserver run on different ports, they can co-exist without any issue.
However, you need to make sure clients have access to both port 80 (usually this is allowed from most firewalls and access restricting methods) and 5000 from client devices.

Django is not available from forwarded port

I'm trying to connect to my website from another node on another network. If the nodes are in the same network, i can connect to the website without a problem.
I've forwarded port for ssh and Django (8000), I also have apache ready on port 9080.
ssh and apache ports work fine when connecting to them from external ip address, Django does not for some reason.
First, i tried to run the server on port 8000:
python manage.py runserver 0.0.0.0:8000
This works completely fine when connecting from the node that is in the same network as the server, but for some reason, whenever i try to access it from external ip address, the connection is refused.
To make sure it was Django, I also tried running the server on the same port as Apache (9080), although, i didn't expect "errorless" response, since i knew that port was occupied. But there was no change at all, I was still getting the same Apache page that i would get before.
I also tried allowing port 8000 on firewall:
sudo ufw allow 8000/tcp
But pretty sure this is not the problem, since this Debian came without any firewall.
I also tried to empty ALLOWED_HOSTS in settings, but there was no progression.
It seems like Django has no effect for external connections, what could be the reason?
I also struggle to understand the purpose of other http web server platforms in this case (e.g Apache, Nginx), Isn't Django creating a webserver itself along with its custom wsgi?
Firewall is not the problem, neither is the web server, then may the problem be caused by the Django itself? Maybe it is outer firewall?
It is not clear how you are configuring Apache to forward requests to Django, it seems like you are treating those as two independent components. If you want to use a web server in front of Django (recommended for production envs), you need to configure both Apache and Django.
Then, as you are running django in dev mode (python manage.py runserver 0.0.0.0:8000), you should reach Django in http://server_ip:8000 regardless of Apache, perhaps there is another firewall blocking the connection. Use tracert / traceroute to find out where the connection is blocked.
FInally, for production environments, it is recommended to use a web server in front of Django to increase security and performance. See the docs for further information.
My guess is that you have another firewall blocking the port. You opened the local firewall using ufw, but there may be an outer firewall.
python manage.py runserver 0.0.0.0:8000 starts correctly? If so, keep an eye in the log.
Inside the server, do a request wget http://localhost:8000. The request should be logged
If you can reach Apache in port 9080 from outside the server, you can:
Use nmap to find the opened / closed / filtered ports in the server to find if there is another firewall inbetween.
Configure Apache to forward requests to Django, although this does not solve the problem
In your question you say that you have forwarded port for ssh and Django. What exactly is this? Are you sure that you have not misconfigured your ssh server to listen in port 8000?

Can't connect to Flask web service, connection refused

I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:
running on http://127.0.0.1:5000/
But when I enter this address on my laptop's in Chrome, I get
ERR_CONNECTION_REFUSED
I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?
Run your app like this:
if __name__ == '__main__':
app.run(host='0.0.0.0')
It will make the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000
when you are running the server via flask run change it to flask run --host=0.0.0.0
to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000
A reason could also be in firewall refusing incoming connections on port 5000. Try:
sudo ufw allow 5000
app.run(host='0.0.0.0',port=5000)
if you run your app in this way then your server will be visible externally.
Steps by Setp:
Run your app by using the following command
app.run(host='0.0.0.0',port=5000)
Go to the window cmd . Type ipconfig and get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X
Go to the mobile browser and type the 192.168.X.X:5000
If you have debug = True inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the debug = True.
You will have to run the development server such that it listens to requests on all interfaces and not just the local one
Ask Flask to listen on 0.0.0.0:PORT_NUMBER
or any other port you may choose
On MacOS 12.4 (Monterey) I couldn't load localhost nor my local IP but it worked with both of these:
0.0.0.0
127.0.0.1
Just change the URL in the browser if it loads with "localhost".
Both devices must be connected to same network.
Use app.run(host='0.0.0.0',port=5000) and run with your own Ipv4
address like his http://[Your Ipv4 address]:5000
If you are connecting this with android app then don't forget to
put INTERNET permission in manifest file.
Contrary to popular believe 127.0.0.1 is not the same a localhost.
I solved the issue above by setting 127.0.0.1 explicitly on both ends.
Well i was also here to know answer but i guess i found out problem. The reason is that you may not activated or run flask that's why it is showing error. For that you have to start from terminal "flask run" and then surely your flask will run...
The issue may occur, if VPN is on, so try to switch it off.

Categories

Resources