How to set up a Python HTTP server

Python is a high-level programming language currently used for a variety of purposes. The source code is easy to read, and typically requires fewer lines than source code written in C. Python has a small HTTP server built in to it, which allows you to use a system directory as a web server directory. The Python HTTP server is most beneficial to users who need a basic HTTP server, but don't want to spend time configuring a larger server such as Apache. If you're using the CentOS operating system and already have Python installed, you just need to start the Python HTTP server and verify that it's working.

DIFFICULTY Basic - 1 | Medium - 2 | Advanced - 3
TIME REQUIRED 10 min
RELATED PRODUCTS Linux-based VPS or dedicated servers

Here is a quick tutorial on how to set up a Python HTTP server.

Install Python

You'll normally install Python as part of your CentOS installation. Verify that Python is running by entering "python" from the command line. This command should produce output similar to the following:

If Python isn't already installed, you can install it with a yum command similar to the following:

$ yum -y install python27 python27-devel

Start Python

Assume for this example that you want /etc to be your HTTP server directory. Go to that directory and start the Python HTTP server with the following commands:

$ cd /etc
$ python -m SimpleHTTPServer

You should see the following message indicating that the HTTP server is serving port 8000 by default:

Serving HTTP on 0.0.0.0 port 8000 ...

Display the server page

Assume that your IP address is 127.0.0.1. Enter the following URL to display the server page: http://127.0.0.1:8000

If the directory in which the server is running doesn't have a file named index.html, the server page shows the contents of the directory. If the directory has a file named index.html, this file will determine the contents of the server page.


Was This Article Helpful?
Thank You For Your Feedback
Glad we helped! Anything more we can do for you?
Sorry about that. How can we be more helpful?