Eight Practical Python CLI -m Tricks Without Writing Code
This article demonstrates eight useful Python command‑line one‑liners that leverage the built‑in -m option to perform tasks such as port testing, launching a web server, formatting JSON, creating a simple editor, building executable packages, encoding/decoding data, retrieving system metadata, and compressing files, all without writing any source code.
Python’s -m option enables powerful one‑liner commands without the need to write scripts; the following eight examples illustrate how to use built‑in modules for common tasks.
0. Python CLI “-m” parameter
Running python --help displays all supported options. The -m mod flag runs a module as a script, allowing immediate use of any module that implements a command‑line interface.
1. Service port testing
Instead of installing telnet , you can use the built‑in telnetlib module to test outbound network ports. Example:
<code>python -m telnetlib -d 142.250.70.174 443</code>Testing an invalid port (e.g., 999) raises an error, confirming the check.
2. Launch a local web server
Python can start a simple HTTP server without any code:
<code>python -m http.server</code>The server listens on port 8000 and serves files from the current directory, useful for quickly sharing files with peers.
3. Validate and format a JSON string
Using the json.tool module you can pretty‑print a JSON string directly from the shell:
<code>echo '{"name": {"first_name":"Chris", "last_name":"Tao"}, "age":33}' | python -m json.tool</code>If the JSON is malformed (e.g., missing a comma), json.tool reports the error, helping you correct it.
4. Create a simple text editor
The idlelib module (based on Tkinter) can launch a minimal UI editor:
<code>mkdir get_time_app && python -m idlelib get_time_app/print_time.py</code>After editing and saving (Ctrl+S), you can view the file with cat get_time_app/print_time.py .
5. Build an executable application
Python’s built‑in zipapp module packages a directory into a self‑contained .pyz executable:
<code>python -m zipapp get_time_app -m "print_time:main"</code>Run the resulting application with python get_time_app.pyz .
6. Encode and decode strings or files
For quick encoding, use the encodings.rot_13 module:
<code>echo "I am Chris" | python -m encodings.rot_13</code>Base64 encoding/decoding is also available via the base64 module:
<code>echo "I am Chris" | python -m base64</code> <code>echo "SSBhbSBDaHJpcwo=" | python -m base64 -d</code>Files can be encoded similarly, and the decoded script can be executed directly.
7. Retrieve system metadata
Running python -m sysconfig prints comprehensive build and configuration information. For a concise view of the environment and site‑packages paths, use:
<code>python -m site</code>8. File compression
The zipfile module can create and extract ZIP archives without external tools:
<code>python -m zipfile -c get_time_app.zip get_time_app</code> <code>python -m zipfile -e get_time_app.zip get_time_app_extracted</code>After extraction, you can verify the contents with ls and cat . The module also supports TAR and GZIP formats.
Summary
The article showcases how Python’s built‑in modules accessed via the -m flag can accomplish a wide range of tasks without writing any code, offering convenient shortcuts for developers in many scenarios.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.