How to make your localhost server accessible anywhere.
Creating a local sever and making it accessible on other computers and phones.
When we develop a server we require to host it to provide it to the client applications. But if one requires testing the server with the client without hosting it then yes there is an option to test the servers with your application locally.
So It is no waste of time to write a few lines of code and test our own server.
The request of all the programmers please develop the hello world!! server with your known programing language. I am Using Python+Flask. so if you guys wants to follow me then your welcom. make sure your have python installed and flask is added to the python env.
Create a python file and write the below code into that.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def get_name():
"""
This is GET method
:return:
"""
return "My Local server!!!"
if __name__ == "__main__":
app.run()
If your python setup is correct and the flask is available, then execute this above code.
The output will be:
Debug mode: off
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I highlighted the port number that is 5000, keep in mind that we are running the server on 5000 port.
Test localhost URL “http://127.0.0.1:5000/” on the browser, it will print “My Local Server!!!” on a web browser as output.
So if you use this URL with your other computer or try with a phone browser It is not going to work there because we can access the localhost within the same computer.
But we have a technique to make our localhost accessible to our other computers.
The tool which I am going to use called ngrok, there are many alternatives also present like.
- Localtunnel, Serveo, Teleconsole, Pagekite, etc.
All the tools are almost the same for this task, I prefer ngrok.
To use ngrok see the following steps.
- Download ngrok form the link. https://ngrok.com/download.
- You will receive a zip file, just unzip it to any location in your PC.
- The above application is ngrok tool that you will see after unzipping the downloaded package, to use it just start cmd with the same location where the ngrok application is stored.
- Type command “ngrok HTTP [PORT]” on cmd, for example, the server we created on port 5000 by default. then > ngrok HTTP 5000.
- Now the command output will look like.
These proxy links will be generated into the tool.
http://b65fc6f21b45.ngrok.io -> localhost:5000
Forwarding https://b65fc6f21b45.ngrok.io -> localhost:5000
- The server is running on http://127.0.0.1:5000/ so replace the HTTP or HTTPS URL instead of the localhost URL.
- Test the above URL now.
- Test the same as the localhost.
- Test on another computer or phone.
Now, this the local server can be accessible anywhere and we can use it like a hosted server.
The concept behind it, All the request comes on “ngrok” URLs will be redirected on our localhost.
This is how we can make a local server accessible anywhere like our local emulators, virtual machine environments, and all.
Thanks for reading!
Happy coding.