How to Access Localhost of a Remote Server


In this article, we will show how to access localhost of a remote server. Localhost corresponds to the hostname of the device that currently runs your application. It is the DNS name for the local loop back address, which is . So, instead of , you type localhost. We use the localhost to access the network services that are running on the machine. This machine may be your computer in front of you or it may be remote server.

If you are running your Python application in your local computer, you just write localhost to your web browser and the output of the Python application that runs a web app is displayed on your screen.

However, if you are running the Python code on a remote server, you need to access the localhost of the remote server. Normally it can‘t be accessed from outside of the local host, so how are we going to do it?

Fortunately, ssh makes it not only possible but also quite easy for us.

Let’s say you want to run localhost:8000

ssh -L 8000:localhost:8000 root@IPOFTHEREMOTESERVER

Now, make sure that your web app is up and running, otherwise you will get the following error every time you try to access the app that is supposed to be run on the localhost.

open failed: connect failed: Connection refused

After firing the above command, you just type localhost:8000 on your local computer and the web app running on the remote server will be displayed on the web browser of your local computer.