How to Start a Simple Web Server in Python 3 on Mac

Jul 30, 2018 - 1 Comment

Start simple web server in Python 3 with http server command

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.

Not to worry, the simple web server Python trick still works in Python 3 for Mac (and for Linux and Windows too of course, but we’re obviously covering MacOS), it’s that the command syntax is just slightly different. We’ll show you how to start a simple web server with Python 3 by using the new Python 3.0+ equivalent of the python -m SimpleHTTPServer command.

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:

http://0.0.0.0:8000

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.

The python3 simple web server with HTTP

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.

Do you know of any other interesting Python tips or tricks? Do you know of any other helpful python command strings for starting http servers or otherwise? Share with us in the comments below!

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

One Comment

» Comments RSS Feed

  1. menxit says:

    An alternative could be:

    “`
    php -S 0.0.0.0:3000
    “`

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site