How to create a simple web server using Python 3 on Mac

If you are familiar with the Python programming language, then you probably know how to create simple web servers in

command line on Mac. However, with the new version of Python 3, the old commands no longer work and the way you create web servers has changed.

Don’t worry, using Python 3 to createweb servers are just as easy, only the teams have changed. We will use MacOS for instructions, but the method works the same on PCs with Windows and Linux. Below we will explain everything in detail.

Advertising

Advertising

How to create a web serverHTTP by usingPython3.0+

Obviously, before proceeding with the instructions, you need to install Python 3.0+ on your Mac. In previous versions, the teams will be different.

Type the following command at a command prompt:

Advertising

Advertising

python -m http.server

OR (depending on what you call Python 3.x):

python3 -m http.server

Press return (enter) and Python 3 will instantly create a simple HTTP web server.

The Python 3 http server will start in the Terminal program if there is no web file in the directory.

You can check it in the browser at the following URL:

http://0.0.0.0:8000

All web server activity, such as access to individual files, folders, directories, etc., will be displayed in the Python window in Terminal in real time.

If you have both Python and Python3 installed, then the command will need to be slightly modified so that it is not used with the previous version. Most often, the command will look like this:

python3 -m http.server

how withPython3looks like equivalentPython -m CGIHTTPServer?

With Python, you can also use a CGI server if you need the equivalent of the python -m CGIHTTPServer command. The command will look like this:

python3 -m http.server –cgi

If the version of Python 3 is installed as an update and is simply called Python, the command will be like this:

python -m http.server –cgi

What to do if an error "/usr/bin/python:No module named http?

If you encounter the error "/ usr / bin / python: No module named http ", most likely, you simply do not have Python 3 active, or you use the wrong command from two options. There is also a chance that the programming language is not installed on the computer at all. On a Mac, this is less likely, because Python2 is the standard programming language on Mac OS. If you installed Python 3.x yourself, it will automatically remove Python 2.x. The process most commonly used is Homebrew.

Try using the python -m SimpleHTTPServer command for Python 2 and earlier

Simple Python web servers are not suitable forfull work, they are more likely to be used for quick testing. If you want to create a full-fledged web server, you need to use Apache or Nginx, although working with them will be more difficult. You can also use MAMP, which makes creating and configuring web servers as easy as possible.