Create a Quick Local Web Server with Python and Ngrok
Tags: ngrok, webdev, python
Published: 2019-05-12 04:50:06

---
title: Create a Quick Local Web Server with Python and Ngrok 
published: true
description: Using two simple tools you can spin up a publicly available web server
tags: ngrok, webdev, python
---

One of the nice things about starting a new job is that you have to setup all of your development tools and environments from scratch. And some of the things you thought you'd never forget stubbornly hide behind the memories that are much easier to recall.

Such was the case for me recently. On a machine that was only a few weeks old, I needed to make an HTML file quickly accessible to the outside world. I had done this dozens of times before, but for the life of me, I could not remember the exact tools I needed to get this job done. 

Then, after much searching, the fog cleared. All I needed was to run a single Python command to fire up a local web server. Then, using a tool called [ngrok](https://ngrok.com/) I could make that server accessible to the world.

This blog post is more of an insurance policy against my faulty memory, so I can easily remind myself of these things in the future. However, you are more than welcome to continue reading if this sounds interesting to you. First, let's get the Python web server running on port 8000.

### Python
A super quick way to fire up a web server, with virtually no configuration (especially if you're on macOS) is to use Python's SimpleHttpServer. In Python 2.7 (which is the default version that comes with macOS) the server is started by running `python -m SimpleHTTPServer 8000`.

In Python 3 this can be done by running `python -m http.server 8000`.

With this command, the `SimpleHTTPServer` will serve up the contents of the current directory on port 8000. Any port can be specified here, but make sure it's one that is not currently in use. The contents can be accessed locally by pointing your browser to [http://localhost:8000/](http://localhost:8000/). Now that the simple web server is running, it's time to configure ngrok to make this server accessible by the Internet at large.

### ngrok
Ngrok is a tool that provides a secure tunnel for a local application to access the Internet, and for the Internet to access it. This tunneling comes in handy when demoing or testing web sites without having to deploy them. For instance, if you're building an application that subscribes to webhooks, and those webhooks need a callback URL to access the application on your machine, ngrok would provide that address for the webhooks to call. In my case, I just needed a simple way to serve up HTML files.

There are a lot of features to ngrok, but this article is only going to focus on providing a simple http tunnel on a specified port. This can be done with the following steps:

* Download ngrok from the [ngrok Download Page](https://ngrok.com/download).
* Extract the ngrok executable and put it somewhere that makes sense to you. I put it in `/Applications/Utilities`.
* To get the `ngrok` executable into `PATH` so that it's executable from anywhere on your system, create a symlink in `/usr/local/bin/` that points to where ever `ngrok` is saved. If it's in the Utilities directory, as mentioned above, the symlink command would look like this `ln -s /Application/Utilities/ngrok /usr/local/bin/ngrok`
* Go the the directory where `SimpleHTTPServer` is running and run the following command
`ngrok http 8000`

The ngrok output then shows the following:

![Screenshot of ngrok](https://thepracticaldev.s3.amazonaws.com/i/7rc3vyqrejou79nz9q33.png)

The important piece you're looking for are the values for `Forwarding`. The local web server will now be visible at the `x.ngrok.io` domains. As requests are made to this tunnel, the results are logged in the Connections section of the ngrok CLI. For the free version of ngrok, this session will last for 8 hours. When the ngrok process expires, or is restarted, the subdomain value in the address will change. If you're looking for a consistent address, I believe you can purchase a license from ngrok.

Now with these two simple tools you can spin up publicly accessible web servers to your heart's content.