How to run SimpleHTTPServer in python3?

Python programming language is widely used in web development specially in backend development. In this Python article, we will learn about “python -m SimpleHTTPServer” and also we will learn What is the equivalent of “python -m SimpleHTTPServer” in Python3.

Table Of Contents

What is “python -m SimpleHTTPServer”?

In Python 2, "python -m SimpleHTTPServer" command is used to start a simple HTTP server using a module named as SimpleHTTPServer which comes pre-installed with Python programming language. This module provides a basic HTTP server for serving content from the current directory.

How to use “python -m SimpleHTTPServer” :

To use this command, open cmd/bash from the current working directory which you want to be served. Then run python -m SimpleHTTPServer command. This will start HTTP server on port 8000 by default. Then open your browser and type: http://loaclhost:8000 to access your content.

Port number can also be changed according to the user. Type Port number you want to use in the place of PORT python -m SimpleHTTPServer PORT to serve files on different port number.

“python -m SimpleHTTPServer” in Python3

"python -m SimpleHTTPServer" is equivalent to http.server in Python3. This “python -m SimpleHTTPServer” has been replaced with http.server from Python version 3.

Using http.server

From Python3 onwards, the http.server is being used to for web client-server communication. The http.server module comes pre-installed with Python3 which provides a basic HTTP Server for serving content from a directory over HTTP.

To start an HTTP server using the http.server module, open a terminal or command prompt and navigate to the directory that you want to serve. Then run the python -m http.server command. This will start the HTTP server on port 8000 by default. You can then access the content in the directory by visiting http://localhost:8000 in a web browser.

You can also specify a different port number by including it as an argument. For example, python -m http.server 8080 will start the HTTP server on port 8080.

Here is an example code to create a server in Python3 Using the http.server module.


# importing required modules.
import http.server, socketserver

# configuring port number.
PORT = 3000

# inheriting from http.server.SimpleHTTPRequestHandler
class MyHandler(http.server.SimpleHTTPRequestHandler):
 def do_GET(self):
 self.send_header("Content-type", "text/html")
 self.wfile.write(b"Serving From Python http.server module.")

# creating a new tcp server.
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
 print("serving at port", PORT)

 # this will start the server.


serving at port 3000

In the above example code and output, you can see an HTTP server has been created on the localhost:3000 by using http.server module which is equivalent of “python -m SimpleHTTPServer”. This serves the files and directories to port 3000 and can be accessed on browser.

Here we have a MyHandler class which inherits from http.server.SimpleHTTPRequestHandler. This class handles incoming HTTPS request.

Here we have a do_GET() method in MyHandler class, which is called by the server when the server receives an HTTP GET request. Then this method sends a response to the client which has a status code and body.

The send_header() method is used to specify the MIME type of the body. Whereas, the end_headers() is used to indicate end of HTTP headers.

Here wfile has been used to send the body to client.

Then we have created a new TCPServer object, using socketserver module. The socketserver module comes pre-installed with Python3 which provides a number of classes that can be used to create network servers.

The TCPServer class is a basic TCP server that listens for the connections and handles them using a separate thread for each client.

Then, The serve_forever() method starts the server and listens for requests indefinitely. To stop the server press CTRL+C in cmd.


So In this Python article, of What is the equivalent of “python -m SimpleHTTPServer” in Python3, we learned that the http.server is equivalent to the “python -m SimpleHTTPServer” in Python3. The SimpleHTTPServer module was used in Python2 for client-server communication. But after Python3, we use http.server module for client-server communication. Also in this article we learned to create a basic server with the help of http.server and socketserver modules.

Make sure to practice with above examples, in order to have a better understanding of this problem. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top