How To Create A Simple HTTP Server Using Python

If you need a simple Web Server instead of installing complex HTTP services like Apache, Nginx, etc. You can use Python’s own package to complete a simple built-in HTTP server. You can then display your directories and files in an HTTP manner.

1. Create HTTP Server Commands.

Python -m Web Server Module [port number, default 8000]

There are three web server module.

  1. BaseHTTPServer: provides basic Web services ( HTTPServer ) and processor classes ( BaseHTTPRequestHandler ).
  2. SimpleHTTPServer: the SimpleHTTPRequestHandler class that contains the GET and HEAD requests.
  3. CGIHTTPServer: contains handling POST requests and executing the CGIHTTPRequestHandler class.

2. Start Web Server.

2.1 Python 3 Start Web Server.

python3 -m http.server 8080

2.2 Python 2 Start Web Server.

python -m SimpleHTTPServer 8080

3. Browse Web Server Content.

Please note after you start the python web server, all the files and directories under your executing folder will be exposed to public to browse through web browser. Open a web browser and browse http://localhost:8080, you can see web page like below.

python built-in web server example