Table of Contents
If you’re a Python user you may already be familiar with the handy trick that allows you to instantly create a simple web server using an easy command string entered in the command line of Mac OS. But if you’re a Mac Python user who has installed or updated to Python 3, then you will find the traditional command string from the prior Python versions does not work to initiate the web server in new Python 3.x+ releases.
How to Start a Web HTTP Server in Python 3.0+
We’re assuming you have already installed or updated to Python 3.0+ on the Mac, this variation of the command requires Python 3.0 or newer.
From the command line, enter the following syntax exactly*:
python -m http.server
OR (depending on how Python 3.x is installed and named):
python3 -m http.server
Hit return and Python 3 will instantly start a simple HTTP server from the directory in which the command was executed.
The http.server in Python 3 will run in the terminal, if there is no web file in the directory than the directory index itself will be shown.
You can test this immediately by opening the following URL in any web browser on the computer:
All web server activity, like accessing individual files, folders, directories, etc, will be shown in the active Python terminal window live as it happens, sort of like tailing web logs on an Apache or Nginx server.
* Important Note: if you have python and python3 installed concurrently, you may need to change the syntax slightly to reference python3 and python2 or another python version. This will depend on how you updated to Python 3 on the Mac, but a common example would be using the ‘python3’ command instead:
python3 -m http.server
Just as before, hit return and the active directory will be turned into a web server.
What’s the Python3 equivalent of python -m CGIHTTPServer?
Another common trick is to use the CGI (Common Gateway Interface) server in Python for CGI scripts in python or perl. Thus if you need to run the python3 equivalent of the “python -m CGIHTTPServer” command for CGI that would be as follows:
python3 -m http.server --cgi
Alternatively, if Python 3 is installed and named as python, the command would simply be:
python -m http.server --cgi
Either way you’ll need the –cgi flag to start the CGI HTTP server in Python 3.
I get an error saying “/usr/bin/python: No module named http” now what?
If you see an “/usr/bin/python: No module named http” error when trying to execute the python -m http.server command string then you most likely are not running Python 3, or you are using the wrong command for python3 (i.e. python vs python3, depending on how the version is named and how it was installed or updated to on the Mac). It’s also possible that Python is not installed on the computer at all, though that’s less likely for Macs because Python2 is installed by default on Mac OS, though users must manually install the updated Python 3.x on the Mac, which simultaneously preserves the original Python 2.x release version. Typically this is achieved with Homebrew.
Change the syntax as referenced above, or if you’re using an earlier Python version try the “python -m SimpleHTTPServer” command from Python 2 and before.
Of course the python simple web servers are not meant for production environments and they’re really just best for a quick scratchpad or test environment. If you want to run a web server that is publicly facing or just more robust in general, you’d want to go with something like Apache or Nginx, though configuring those individually on a Mac requires some setup. A simpler option for an full web server environment on the Mac is to use MAMP, which makes setting up and starting a full Apache, MySQL, PHP environment on the Mac about as simple as it gets.