| Start |
|---|
Running a simple HTTP server Running this command serves the files of the current directory at port If no argument is provided as port number then server will run on default port The If you want to only serve on localhost you'll need to write a custom Python program such as: |
| Serving files Assuming you have the following directory of files: You can setup a web server to serve these files as follows: The
The 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. Open localhost:8000 (opens new window) in the browser, it will give you the following: |
| Programmatic API of SimpleHTTPServer What happens when we execute To answer this question we should understand the construct of SimpleHTTPServer (https://hg.python.org/cpython/file/2.7/Lib/SimpleHTTPServer.py) (opens new window)) and BaseHTTPServer(https://hg.python.org/cpython/file/2.7/Lib/BaseHTTPServer.py) (opens new window)). Firstly, Python invokes the 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: The links https://hg.python.org/cpython/file/2.7/Lib/BaseHTTPServer.py (opens new window) and https://hg.python.org/cpython/file/2.7/Lib/SocketServer.py (opens new window) are useful for finding further information. |
Basic handling of GET, POST, PUT using BaseHTTPRequestHandler Example output using |
Read Article
https://develop.horje.com/learn/1434/reference