Python provides us with various modules to work on different tasks. If you want to create a simple web server in Python to serve files, you can use the Python SimpleHTTPServer module. In this article, we will discuss the basics of Python SimpleHTTPServer and how it works.
What is Python SimpleHTTPServer?
The SimpleHTTPServer module that comes with Python is a simple HTTP server that
provides standard GET and POST request handlers. You can easily set up a server on localhost to serve files. You can also write HTML files and create a working web application on localhost with the SimpleHTTPServer module.
Why Should You Use Python SimpleHTTPServer?
An advantage of the 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. That makes it perfect to use when you need a quick web server running and you don’t want to mess with setting up Apache or Ngnix-like servers.
SimpleHTTPServer is a simple and efficient tool to learn how a server or a web app works using GET requests and POST requests. You can use this to turn any directory in your system into your web server directory.
How do I Use Python SimpleHTTPServer?
To start an HTTP server on port 8000 (which is the default port), you can simply the following command in the command prompt.
python -m SimpleHTTPServer
The above command works for Python 2. To run SimpleHTTPServer in Python 3, you need to execute the following command.
python -m http.server
After execution of the above command, the python simpleHTTPserver will be started on your machine. You can observe this in the following example.
In the above image, you can observe that the server has started on the 8000 port.
To access the server using the web browser you can open the link localhost:8000
or 127.0.0.1:8000
on your web browser. There, you will find all the files of the directory in which the SimpleHTTPServer has been started. You can click on any file or directory to send a GET request to the server to access the files as shown below.
In the above video, you can observe that all the files in the directory in which the simpleHTTPserver is started are displayed in the browser. When we click on the httpserver_example
folder, the browser issues a GET request. Then, the server displays the contents of the folder. Again, when we click on the files in the httpserver_example
folder, the file contents are displayed on the screen. If a file cannot be displayed, it will be downloaded through your browser. Thus, the Python simpleHTTPServer module makes your file system into a temporary web server.
In the terminal where you started the server, you can observe the following outputs.
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [17/Jun/2023 10:46:52] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2023 10:47:29] "GET /httpserver_example/ HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2023 10:47:29] "GET /favicon.ico HTTP/1.1"
127.0.0.1 - - [17/Jun/2023 10:47:32] "GET /httpserver_example/pfb.html HTTP/1.1" 200 -
127.0.0.1 - - [17/Jun/2023 10:47:38] "GET /httpserver_example/pfb.txt HTTP/1.1" 200 -
In the terminal output, you can observe that the browser has issued a GET request every time we clicked on a folder or file name in the browser.
Instead of using the default 8000 port for running the simpleHTTPserver, you can specify the port number after the SimpleHTTPServer command as shown below.
$ python -m SimpleHTTPServer 8080
After execution of the above command, the Python SimpleHTTPServer will run on port 8080 instead of the port default port.
In Python 3, you can specify the port for running the Python simpleHTTPserver as shown below.
python -m http.server 8888
After executing the above command, the simple HTTP server will be started on the 8888 port instead of the 8000 port. You can observe this in the following image.
Suggested Reading: To read about how to write Python programs to serve files with custom paths using SimpleHTTPServer, you can read this article on SimpleHTTPServer with Default and Custom Paths.
How to Share Files and Directories Using SimpleHTTPserver?
To share files and directories using SimpleHTTPServer. you first need to move to the directory whose contents you want to share. For this, you can open a terminal and cd into whichever directory you wish to have accessible via browsers and HTTP. After that, you can start the server in the given directory.
cd /var/www/
$ python -m SimpleHTTPServer
After you hit enter, you should see the following message:
Serving HTTP on 0.0.0.0 port 8000 …
Then, you can open your favorite browser and put in any of the following addresses:
http://your_ip_address:8000
http://127.0.0.1:8000
If you don’t have an index.html file in the directory, then all files and directories will be listed as shown in the video example.
As long as the HTTP server is running, the terminal will update as data are loaded from the Python web server. You should see standard HTTP logging information (GET and PUSH), 404 errors, IP addresses, dates, times, and all that you would expect from a standard HTTP log as if you were tailing an Apache access log file.
SimpleHTTPServer is a great way of serving the contents of the current directory from the command line. While there are many web server software out there (Apache, Nginx), using Python
built-in HTTP server requires no installation and configuration.
Conclusion
In this article, we have discussed the basics of Python SimpleHTTPServer. To learn more about Python programming, you can read this article on string manipulation in Python. You might also like this article on list comprehension in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.