Python3 SimpleHTTPServer: How to Use SimpleHTTPServer
Python3 SimpleHTTPServer module is an inbuilt HTTP server in which you don’t have to install and configure anything. SimpleHTTPServer is a very convenient tool. You can use the Python SimpleHTTPServer to turn any directory into a simple HTTP web server. SimpleHTTPServer module that comes with a simple HTTP server that provides standard GET and HEAD request handlers.
One advantage of the inbuilt HTTP server is that you don’t have to install and configure anything. The only thing that you need is to have Python installed.
#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:
- Listens for the incoming http requests on a specific TCP socket address (IP address and a port number)
- It handles this request and sends a response back to the user.
When you open your Chrome browser and type google.com in the address bar, you will get the Google home page rendered on your browser window.
But what just happened under the hood?
A lot of 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 google.com on your browser, your browser will create a network message called an HTTP request.
This HTTP Request will travel to a Google computer that has 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, and that’s what 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 Example
Web servers are everywhere. Even you are interacting with one right now!
No matter what type of software engineer you are, at some point in your path, you will have to deal with the web servers.
Maybe you are building the API server for a backend service, or perhaps 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, then 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.
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. If you want to start a server on default port 8000, then type the following command.
python -m SimpleHTTPServer
You can change the default port bypassing the port in the argument in the above command.
python -m SimpleHTTPServer [port]
We have started the Python SimpleHTTPServer on port 8000.
Now, if you will go to the browser, then it will show all the files and folders list in the output of a webpage.
#No module named SimpleHTTPServer[Solved]
No module named SimpleHTTPServer error is ModuleNotFoundError in Python.
If you are using Python3 and if you try to start the SimpleHTTPServer, then 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
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"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h3>Python SimpleHTTPServer</h3> <p style="margin-left: 30px; color: rebeccapurple">Python 3 Web Server</p> </body> </html>
Now, go to the browser and type this URL: http://localhost:9000 and see the output.
#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 app.py file.
# app.py import http.server import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()
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. Python http server module doesn’t show all the python modules details on quitting from 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. It removes the laborious process associated with installing and implementing the available cross-platform web servers.
If you are using Python 2, then you can use the following command.
python -m SimpleHTTPServer 8000
If you are using Python 3, then you can use the following command.
python3 -m http.server 8000
The server provides a simple directory UI in which you can 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 be used to 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.
Finally, Python3 SimpleHTTPServer Example is over.