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.

  1. The below command will create an HTTP web server.
    Python -m Web Server Module [port number, default 8000]
  2. There are three web server modules.
  3. BaseHTTPServer: provides basic Web services ( HTTPServer ) and processor classes ( BaseHTTPRequestHandler ).
  4. SimpleHTTPServer: the SimpleHTTPRequestHandler class that contains the GET and HEAD requests.
  5. CGIHTTPServer: contains handling POST requests and executing the CGIHTTPRequestHandler class.

2. Start Web Server.

  1. The below command starts the webserver in python3. The web server listening port number is 8080.
    python3 -m http.server 8080
  2. The below command starts the webserver in python2.
    python -m SimpleHTTPServer 8080

3. Browse Web Server Content.

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

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky