How to access ports on a local server via ssh? [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 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.

Related

Can't connect to GCP VM website with external IP

Trying to connect to my django website (from the browser) that's stored on the GCP virtual machine.
Everything works fine if I'm accessing it internally using internal IP or localhost.
However, I can't access website with external IP.
No logs in django that would say someone trying to access if I'm trying with external IP.
I have http, https traffic enabled on instance.
Firewall rule to allow port 80:
Here is Test-Net results.
Searched the web for answers but nothing looks wrong in my settings..
Any ideas would be appreciated.
UPDATE:
Do not create or change egress rules unless you know exactly what they do. They are not necessary for ingress rules (VPC Firewalls automatically allow return traffic):
I've changed all firewall rules back how they were so now only port 80 is allowed.
You have an ingress rule for the target http-server. Is that target flag set on the VM instance?
What is the output from sudo lsof -i -P -n | grep LISTEN? Your Django server must be listening on 0.0.0.0.0 instead of localhost.
I have 0.0.0.0 with port 80 at django terminal.
I use windows 2016 server so don't know the powershell function that would display what you asked for.
Here is netstat listening ports for django.

Node.js runserver on GCP Virtual Machine

I have a python application hosted by a node.js frontend. I am running that on a linux vm on Google Cloud virtual machine (GCP).
node appname runserver 8080 command starts local server within VM but I am wondering what would be step by step process to access it via a DNS from outside world.
Or if there is better approach to host python ML applications behind a web interface, then please suggest.
You need to use forever for this.
Forever will move the node process to the background and service will keep running in the background even if you log out of the server. And In order to access from outside point a DNS domain to this IP address of the machine and then Proxy Pass the request on port 80 to the port your service is running on.
Then you will be able to access it via domain name.
Look for ProxyPass directive in the Http server. That would work for you. :D

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)

Flask application cannot be exposed on droplet

I'm deploying a flask server to a Digital Ocean droplet.
from flask import Flask
app = Flask(__name__)
#app.route("/a/<string:b>")
def deploy(b):
return "Response"
Using the following command:
FLASK_APP=server.py python -m flask run --host=0.0.0.0 --port=5555
When I deploy the application locally, I can receive response by doing
curl -XGET localhost:5555/a/random
When deploying on the droplet, it works internally, but when calling the droplet externally (despite having exposed port 5555 on TCP) it does not connect.
What could have changed? I'm also deploying a flask graphql server on the same droplet via docker which works perfectly fine.
This is possible a common issue when uses VPS.
People like me may often forget to setup firewall(s) if the codes are correct.
You mentioned that it works locally but not externally. I guess it should be.
Digital Ocean level:
Add inbound TCP port 5555 in droplet firewall setting
System level
iptables: e.g. iptables -A INPUT -p tcp -dport 5555 -j ACCEPT
firewalld: e.g. firewall-cmd --permanent --zone=public --add-port=5555/tcp
Or you may disable OS firewalls by systemctl stop [service-name] or service [service-name] stop. You may google the commands.
Flask accepts connections from localhost by default.
In order to make your Flask app publicly available, you have to bind it to 0.0.0.0 address by adding --host=0.0.0.0 parameter.
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.

Can't connect to remote django server port 80

I'm having an issue running and connecting to my python django server on a windows 2012 server. To run the server I use command: python manage.py 0.0.0.0:80. This results in an error below
[Error 10013]: an attempt was made to access a socket in a way forbidden by its access permissions
I've tried running the command prompt as an administrator with no change. For reference, I am able to run the server on port 8000 but then I cannot connect to the port remotely. I have turned off firewalls as well so that is probably not the issue.
While it is preferable to run the django on port 80, I am trying to get this working on any port.
Port 80 (and many other ports) is reserved by Windows Server. Checkout this https://serverfault.com/questions/633179/cant-make-confluence-run-on-port-80-with-windows-server-2012-r2,
You may want to google "Windows Server Reserved port" for more info and a way to "unreserve" it.

Categories

Resources