DEV Community

Scott McAllister
Scott McAllister

Posted on

Create a Quick Local Web Server with Python and Ngrok

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 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.


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/. 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 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.
  • 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

The important piece you're looking for are the values for Forwarding. The local web server will now be visible at the 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.

Top comments (7)

kyletaylored profile image
Kyle Taylor

If you're using Homebrew, a good ol' fashion brew install ngrok should work if you don't want to download and move things.

And for others who might be using PHP regularly, you can also use PHP's built in local server in a directory using php -S localhost:3000

Great tips!

alexdlaird profile image
Alex Laird

This is great!

If you want even quicker access to tunnels alongside like direct access to the ngrok API and all its features through Python, check out pyngrok: Full disclosure, I am its developer. Hope others also find it useful!

dsturbd profile image

This is a great tip. How secure is ngrok?

stmcallister profile image
Scott McAllister

I didn't feel qualified to answer,so I forwarded your question to Twitter, and got the following response that made a lot of sense.

dsturbd profile image

hey thanks for the link Scott, appreciate it

rcaleb68 profile image
Felipe R Valera Mo..

What is the advantage to use python instead of apache server, if any?

stmcallister profile image
Scott McAllister

Honestly, just presenting it as a quick alternative for development. I found it easier to just run the Python SimpleHTTPServer from the directory I wanted to serve up, rather than remembering how to configure Apache and where to put all my files for Apache to serve them up. But, if you're familiar with Apache, and already have that configured, I'd go with that.

Thanks for the question, and for reading my article!