HTTP server outside localhost using NGROK
Last Updated :
01 Sep, 2023
An HTTP (Hypertext Transfer Protocol) server is a program that handles incoming HTTP requests from clients and sends responses back to them. HTTP is the protocol used for communication on the World Wide Web, and HTTP servers are essential components of the web infrastructure.
When a user types a URL (Uniform Resource Locator) into their web browser, the browser sends an HTTP request to the server specified in the URL. The HTTP server processes the request and sends a response back to the client, typically in the form of an HTML document or other web content.
Working of HTTP ServerPython's http.server
Python comes with a built-in module called http.server that allows you to create a simple HTTP server. This module provides classes for implementing HTTP servers (Web servers) that can be used to serve static files or dynamic content generated by Python scripts.
What is ngrok?
Ngrok is a tool that allows you to expose a local web server to the internet. It creates a secure tunnel from a public endpoint (generated by Ngrok) to a locally running web service, allowing you to access the local server from the internet.
Ngrok works by creating a secure tunnel between your local server and a public endpoint provided by Ngrok. You start by downloading and installing the Ngrok client on your local machine and then running a command that creates a tunnel to your local server's port.
How Ngrok Works?
Ngrok is able to bypass NAT Mapping and firewall restrictions by creating a long-lived TCP tunnel from a randomly generated subdomain on ngrok.com (e.g. 3gf892ks.ngrok.com) to the local machine.
We can start a TCP connection with just one command of ngrok and it makes us a long tunnel bypassing any firewall/NAT.
Working of NgrokSecurity in Ngrok Service
Ngrok Secure Tunnels allow you to instantly open access to remote systems without touching any of your network settings or opening any ports on your router. This means we get a secure, reliable tunnel for your developer box, IoT device, or just about anything that has access to the internet. When a user hits the public ngrok endpoint, the ngrok edge figures out where to route the request and forwards it over an encrypted connection to the locally running ngrok agent. From there, the local ngrok agent takes care of sending traffic to your upstream service. The communication between the ngrok edge and the agent is secure and encrypted. Traffic from the user to the ngrok edge, and from the ngrok agent to the upstream service rely on the protocol you are using for encryption. For protocols that support end-to-end encryption using TLS, we provide a TLS tunnel option.
Starting the Ngrok service outside LAN
Follow the steps below to start the http server outside the LAN.
Step 1: Download Ngrok by using the link below.
Visit the official download page of the ngrok service and download ngrok for Kali Linux {https://ngrok.com/download}
https://ngrok.com/download
It will start downloading the zip file.
Ngrok downloaded fileAfter that let's extract the file, after extracting you will see the ngrok file as shown in the below screenshot.
Ngrok Executable FileNow we have to give an auth key to run the ngrok service, it will be on the downloaded dashboard. Login to the ngrok website or if you are newly create an account.
Ngrok auth tokenCopy this command and run it in your terminal with ./ preeceeding it:
Example:
./ngrok config add-authtoken <your authtoken>
Adding auth-token to connect ngrok with the accountNow, let's check if we have set up the ngrok service in the right way or not,
./ngrok http 8080
Create a http serverLet's start a TCP connection in your computer at port 8080.
If you succeed you will get this message with a URL of port forwarding.
Successful TCP Connection So, let's start our http server, for this, we will need python3, so download the Python3 language using the below command:
sudo apt install python3
Step 2: Setting up Ngrok for the http server
Let's assume there is a file that we have to share with another system that is not in the same network. First of all, we will create the ngrok service for http at port 8080(port 8080 is for the web interface).
Open up the terminal where ngrok is downloaded and type:
./ngrok http 8080
This command will open up an http port at 8080 in your system allowing all http connections through it. Learn more about http connection here. As soon as you see the session status online, know that a port at 8080 has started an http server outside your network and will allow connection outside your network. The forwarding link provided will be its access key whoever clicks on that link will enter your system through an http port.
Ngrok Tunnel StartedStep 3: Setting up http.sever with python3
Launch the terminal in the directory we want to share and execute the below command in the terminal.
python3 -m http.server 8080
The server is started in the ngrok tunnelNow go back to the ngrok terminal and copy the link forwarding.
Copy the link to access the fileNow you can share the link with other users and they can access the shared folder.
Shared directory Here you can see the URL given by ngrok redirects us to the shared folder through the Python http server. It is well-secured and is closed instantly after use without leaving any traces behind.
Alternatives for Ngrok
- LocalTunnel: To host your local web applications over the cloud and access the app from a publicly accessible web URL, you can use the free tunneling technology known as Localtunnel. It is considered the best alternative to Ngrok.
- Serveo: Another user-friendly option for Ngrok is Serveo. You can build local tunnels using it without having to install any additional third-party software. With Serveo, the SSH server provides a port forwarding feature for the locally hosted application.
- Teleconsole: Teleconsole is another well-known alternative to Ngrok. It allows free service to help users share their terminal sessions with others. By generating a unique session id, Teleconsole enables you to share your terminal session over the Internet in contrast to HTTP / HTTPS. It must be used carefully and should only be disclosed to someone you can completely trust because doing so is equivalent to giving them full access to your terminal.
Conclusion
Never leave the Ngrok running continuously because doing so puts your privacy in danger. When the shared process is finished, press Ctrl+C to end the connection with both ngrok and the Python HTTP server.
Similar Reads
Calling Web Service Using Curl With Telnet Connection Prerequisite: Telnet, Curl Terminal Network as the name suggests is an application layer protocol used to connect with hosts (remote machines) over a TCP/IP network. It is based on a client-server architecture where the telnet client makes a connection with the telnet server and once the connection
3 min read
How To Install Ngrok & Setup the Auth-Token on Linux? Ngrok is a tool that allows you to create a secure tunnel from a public endpoint to a locally running web service on your machine. In simpler terms, ngrok lets you expose a web server running on your local machine to the internet. So others can access what you are running locally. For example, if yo
4 min read
What is Local Host? When you call an IP address on your computer, you try to contact another computer on the internet, but when you call the IP address 127.0.0.1, you are communicating with the local host. Localhost is always your computer. Your computer is talking to itself when you call the local host. Your computer
8 min read
Exposing Localhost Server over Internet Using ngrok and VS Code We setup and build many projects but before moving them to the production server, we always test the project, and we share the project among our team to share the progress, we share our project code on GitHub as it supports version control system and, but what if we do not want to share the code, bu
3 min read
How to allow access outside localhost in AngularJS ? In this article, we will see how to allow access outside the localhost in AngularJS, along with knowing the basic implementation through the example. Firstly, we will discuss how we can build an Angular application, run it, and preview it in the browser. We can easily do this using the CLI, provided
4 min read
How to deploy web applications using Nginx on Remote server (Ubuntu based) - Set 2 In the last article, we got the idea about Nginx and how we can leverage its benefits, further we installed it on our device or remote server. In this article, we will see how to deploy a web application stored in the remote server using Nginx. We need to follow the given steps in order to deploy we
5 min read
Localhost 8080 Localhost:8080 is a port used by the web servers. It is a local development server that runs on port 8080. It is used in cases when the localhost:3000 and localhost:5000 are busy running some other applications. Developers use localhost:8080 to see the preview of the developed application and test i
3 min read
Launch an HTTP Server with a Single Line of Python Code Python, with its built-in capabilities, offers a simple and effective way to do this. With just a single line of code, you can launch an HTTP server to serve files from your local directory. This feature can be incredibly handy for developers, educators, and anyone needing a quick and easy way to sh
2 min read
How to Deploy Python WSGI Apps Using Gunicorn HTTP Server Behind Nginx This article describes how to deploy a Python WSGI application using Gunicorn and Nginx. Before proceeding with the tutorial, it's a good idea to first familiarize yourself with the terms WSGI, Gunicorn, and Nginx. Web Server Gateway Interface or WSGI is a specification that describes how a web serv
5 min read
Setting up a simple HTTP server using Python In this article, we are going to learn how to set up a simple and local HTTP server using Python. An HTTP server can be very useful for testing Android, PC or Web apps locally during development. It can also be used to share files between two devices connected over the same LAN or WLAN network. Inst
2 min read