Firstly, Python invokes the SimpleHTTPServer module with 9000 as an argument. Now observing the SimpleHTTPServer code,
The test function is invoked following request handlers and ServerClass. Now BaseHTTPServer.test is invoked
Hence here the port number, which the user passed as argument is parsed and is bound to the host address. Further basic steps of socket programming with given port and protocol is carried out. Finally socket server is initiated.
This is a basic overview of inheritance from SocketServer class to other classes:
Running this command serves the files of the current directory at port 9000.
If no argument is provided as port number then server will run on default port 8000.
The -m flag will search sys.path for the corresponding .py file to run as a module.
If you want to only serve on localhost you'll need to write a custom Python program such as:
Assuming you have the following directory of files:
You can setup a web server to serve these files as follows:
The SocketServer module provides the classes and functionalities to setup a network server.
SocketServer's TCPServer class sets up a server using the TCP protocol. The constructor accepts
a tuple representing the address of the server (i.e. the IP address and port) and the class that
handles the server requests.
The SimpleHTTPRequestHandler class of the SimpleHTTPServer module allows
the files at the current directory to be served.
Save the script at the same directory and run it.
Run the HTTP Server :
python -m SimpleHTTPServer 8000
python -m http.server 8000
The '-m' flag will search 'sys.path' for the corresponding '.py' file to run as a module.