Python2 provides the
SimpleHTTPServer module to create a basic HTTP server in the specified folder. The SimpleHTTPServer module is very useful for basic operations like testing, development, learning, etc. The SimpleHTTP server provides a built-in module and there is no need for an installation. With Python3 this SimpleHTTPServer is implemented as “http.server” which is provided under the
Python2 provides the SimpleHTTPServer to server content via the current working directory. The content can be an HTML page a file etc. It is very useful and an HTTP server can be created with a single line of Python script.
$ python2 -m SimpleHTTPServer
As we can see the SimpleHTTPServer listens to all IP addresses via the
0.0.0.0 IP address and use the “8000” port number by default. The web browser can be used to connect via localhost or the IP address of the host to list directories provided via the HTTP server.
With Python3 the SimpleHTTPServer changed a bit and merged into the
http module. The SimpleHTTPServer functionality and more can be used via the “http.server”. The usage is the save where the “http.server” is provided as a module to the Python3 interpreter.
$ Python3 -m http.server
The requests are printed as log messages. Log messages are displayed in a structured manner similar to the web servers like Apache, Nginx, etc. The log messages provide the client’s IP address, request time, HTTP request details like URL, HTTP status code, etc.
127.0.0.1 - - [14/Aug/2021 10:47:06] "GET / HTTP/1.1" 200 -
Redirect Request Logs To Log File
The request logs created by the Python3 http.server can be redirected into a log file to collect logs in a centralized log system or SIEM. The redirection operator provided by the bash can be used to redirect logs to a file like http.log.
$ python3 -m http.server > http.log
Specify Listening Port Number Other Than 8000
The Python3 http.server listen to port 8000 for all interfaces by default. We can use a different port number than 8000 and specify it via the command-line interface. The port number can be added to the command as the last parameter. In the following example, we set the port number as “8080”.
$ python3 -m http.server 8080
Specify IP Address To bind
The Python3 http.server listens to all interface and IP addresses by default. This can create some security problems if it is bind to the public IP addresses. We can specify the IP address to bind to the Python3 http.server after the command.
$ python3 -m http.server 192.168.1.10
Alternatively, we can add the different port numbers to listen like 8080 after the IP address by separating with double colons.
$ python3 -m http.server 192.168.1.10:8080