Member-only story
Built-in Python Modules and Command-line Interfaces
This article explores interesting built-in Python features that could be useful for Python and non-Python developers.
Getting Started
With Python installed, we can use many of the built-in modules and command-line interfaces without any coding.
We can view more information on a particular command-line interface using the -h
option.
For example, to view the help information of the HTTP server module.
python -m http.server -h
HTTP Server
With a one-liner, we can use Python to create an HTTP server.
This command serves files relative to the current directory, using port 8000.
python -m http.server
The server listens to port 8000 by default. The default can be overridden by passing the desired port number as an argument.
python -m http.server 9000
By default, the server binds itself to all interfaces. The option -b/--bind
specifies a specific address to which it should bind. Both IPv4 and IPv6 addresses are supported.
For example, the following command causes the server to bind to localhost only.
python -m http.server --bind 127.0.0.1
By default, the server uses the current directory. The option -d/--directory
specifies a directory to which it should serve the files.
For example, the following command uses a specific directory.
python -m http.server --directory /tmp/
By default, the server is conformant to HTTP/1.0. The option -p/--protocol
specifies the HTTP version to which the server is conformant.