Python3 SimpleHTTPServer: Complete Guide

Python3 http server example

Web servers are everywhere. Even you are interacting with one right now!

No matter what type of software engineer you are, you will have to deal with the web servers at some point in your path.

Maybe you are building the API server for a backend service, or you are just configuring a web server for your website.

If you want to test your local static files quickly and don’t want Apache’s mess, Python can help you create a server in no time; all you need is Python installed on your system.

Python comes with a built-in module known as SimpleHTTPServer, which in other words, is a simple HTTP server that gives you standard GET and HEAD request handlers.

One advantage of a built-in HTTP server is that you don’t have to install and configure anything. The only thing that you need is to have Python installed. 

If you are using Python 2, then SimpleHTTPServer is useful. However, if you are using Python3 in your machine, you need to use http.server module.

What is an HTTP Server

An HTTP web server is nothing but the process that is running on your machine and does precisely two things:

  1. Listens for the incoming http requests on a specific TCP socket address (IP address and a port number)
  2. It handles this request and sends a response back to the user.

When you open your Chrome browser and type in the address bar, you will get the Google home page rendered on your browser window.

But what just happened under the hood?

Many things have happened, but for the sake of simplicity, I will abstract away some of the details and talk about this at a very high level.

At a high level, when you type on your browser, your browser will create a network message called an HTTP request.

This HTTP Request will travel to a Google computer with the webserver running on it.

This HTTP web server will intercept your request and handle it by responding with the HTML of the Google home page.

Finally, the browser renders this HTML on the screen, which you see on your screen.

Every interaction with the Google home page after that (for example, when you click on a link) initiates a new request and response precisely like the first one.

To reiterate, the machine that receives an http request has the software process called a web server running on it.

That web server is responsible for intercepting these requests and handling them appropriately.

Python3 SimpleHTTPServer

Python3 SimpleHTTPServer is a built-in HTTP server in which you don’t have to install and configure anything. Therefore, SimpleHTTPServer is a very convenient tool. You can use the Python SimpleHTTPServer to turn any directory into a simple HTTP web server. SimpleHTTPServer module comes with a simple HTTP server that provides standard GET and HEAD request handlers.

SimpleHTTPServer module can turn any directory of your system into a web server. You need to type a single line command in your terminal to implement this HTTP server.

Python SimpleHTTPServer supports only two HTTP methods – GET and HEAD. So it’s an excellent tool to share files over the network.

Python SimpleHTTPServer has been migrated to python http.server module in Python 3, we will learn about both of these modules today and see how easy it is to work with them. For example, type the following command if you want to start a server on default port 8000.

python -m SimpleHTTPServer

You can change the default port by bypassing the port in the argument in the above command.

python  -m SimpleHTTPServer [port]

Python SimpleHTTPServer Tutorial

We have started the Python SimpleHTTPServer on port 8000.

Now, if you go to the browser, it will show all the files and folders list in the output of a web page.

No module named SimpleHTTPServer[Solved]

No module named SimpleHTTPServer error is ModuleNotFoundError in Python.

If you are using Python3 and try to start the SimpleHTTPServer, you will get the error like No module named SimpleHTTPServer. 

It is because it is merged with http.server module. You can use the below command to run the python http server in Python 3.

python3 -m http.server 9000

No module named SimpleHTTPServer

Now, create the simple index.html file inside that server directory where you have started the server and write the following code inside the index.html file.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <h3>Python SimpleHTTPServer</h3>
    <p style="margin-left: 30px; color: rebeccapurple">Python 3 Web Server</p> 

Now, go to the browser and type this URL: http://localhost:9000 and see the output.

Python SimpleHTTPServer Tutorial With Example | HTTP request Handler

Python3 HTTP server example

Python 3 http server module defines classes for implementing HTTP servers or Web servers. The http.server is not recommended for production. It only performs necessary security checks.

We can write the server code to create a web server. 

Write the following code inside the file.


import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)

Okay, now run the file. So go to the terminal and type the following command.


If you have an index.html file in your directory, then the server will respond with that file.

You can find more about Python 3 http.server on this link.

Python 3 http server module is cleaner provides clear messages. However, the python http server module doesn’t show all the python modules details on quitting the keyboard; that is a more clean approach.


Python’s SimpleHTTPServer module is a handy and straightforward tool that developers can use for several use-cases, with the main one being that it’s a quick way to serve files from the directory. In addition, it removes the laborious process associated with installing and implementing the available cross-platform web servers.

If you are using Python 2, you can use the following command.

$ python -m SimpleHTTPServer 8000

If you are using Python 3, you can use the following command.

$ python3 -m http.server 8000

The server provides a simple directory UI to access any of the files. This is the simplest way to serve files locally over HTTP directly.

Python provides us with the SimpleHTTPServer module (or http.server in Python 3) that can quickly and efficiently serve files from a local directory via HTTP. This can be used for many developments or other internal tasks but is not meant for production.

This is an excellent solution for local use since web servers such as Apache, Monkey, and Jigsaw are much more challenging to get set up and are often overkill for development activities.

That’s it for this tutorial.

Recommended Posts

Python Subprocess

Python Random Number

Python Requests

Python OS Module

Python Collections Module


Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.