How to display the webapp which is running in local host of a server in my laptop's browser

Viewed 342

I have to say that, I am not a web developer and I don't know anything about how a web application works.

I've connected to a remote server via ssh, downloaded a project (Python, flask app) from github and run it there:

zwlayer@personalcomputer $ ssh myusername@ku.edu
myusername@ku.edu $ git clone https://github.com/.../project
myusername@ku.edu $ cd project
myusername@ku.edu $ env FLASK_APP=app.py FLASK_ENV=development env USE_CUDA=False flask run --host=0.0.0.0

Now, is it possible to get interact with that through my browser from my personalcomputer ?

2 Answers

You can use local port forwarding in SSH.

SSH to the server with command:

ssh -L 5000:127.0.0.1:5000 myusername@ku.edu

This will forward port 5000 on your host to port 5000 on the server

Run app with command:

flask run --host=127.0.0.1 --port=5000 (host and port options are used for the purpose of explicitness)

and you should be able to access the application by entering http://127.0.0.1:5000 in your browser.

Read more at https://www.booleanworld.com/guide-ssh-port-forwarding-tunnelling/

When starting a Flask development server using flask run, it starts listening on the loopback interface on port 5000 by default. By adding --host=0.0.0.0 you make the flask server listen on all network interfaces of the host. So, if you have full network access to the host, you should be able to point your browser to it. With the addresses given in your question, just enter http://ku.edu:5000 into the address bar of your browser to interact with your Flask web application.

However, this is not recommended, for security reasons! Since the server is listening on every network interfaces, any person with network access to the host computer can access the application. You're running the application with the Flask development server in development mode, which is not safe for production use.

Instead, I would suggest to use ssh port forwarding to access the flask development server, bound to the loopback interface of the remote host:

zwlayer@personalcomputer $ ssh -L 5000:localhost:5000 myusername@ku.edu
myusername@ku.edu $ git clone https://github.com/.../project
myusername@ku.edu $ cd project
myusername@ku.edu $ env FLASK_APP=app.py FLASK_ENV=development env USE_CUDA=False flask run

This way, ssh forwards all traffic directed to port 5000 of your local computer through the ssh tunnel to localhost:5000 at the remote host, i.e. to port 5000 of the remote host itself.

So, you can point your browser to http://localhost:5000, which is forwarded to the remote server's localhost:5000, which is where your flask server is listening. But—in contrast to the above solution—only local or ssh users at the remote host can access your application.

Related