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.
- The below command will create an HTTP web server.
Python -m Web Server Module [port number, default 8000]
- There are three web server modules.
- BaseHTTPServer: provides basic Web services ( HTTPServer ) and processor classes ( BaseHTTPRequestHandler ).
- SimpleHTTPServer: the SimpleHTTPRequestHandler class that contains the GET and HEAD requests.
- CGIHTTPServer: contains handling POST requests and executing the CGIHTTPRequestHandler class.
2. Start Web Server.
- The below command starts the webserver in python3. The web server listening port number is 8080.
python3 -m http.server 8080
- The below command starts the webserver in python2.
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 the public to browse through the web browser.
- Open a web browser and browse the URL http://localhost:8080, you can see the directory listing web page.