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 HEAD 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), simply type:
python -m SimpleHTTPServer [port]
The above command works for Python 2. To run SimpleHTTPServer in Python 3, you need to execute the following command.
python -m http.server [port]
After execution of the above command, you can open the link localhost:8000 in 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.
You can also change the port to something else:
$ python -m SimpleHTTPServer 8080
After execution of the above command, the Python SimpleHTTPServer will run on port 8080 instead of the port default port.
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?
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.
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 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.