Fundamentals 9 min read

Python CLI Tricks: Using -m Modules, Port Testing, Simple Web Server, JSON Formatting, Text Editor, Executable Packaging, Encoding/Decoding, System Info, and File Compression

This article demonstrates practical Python command‑line techniques—including the -m option for running modules, testing network ports, launching a quick HTTP server, formatting JSON, creating a minimal text editor, building executable packages, encoding/decoding data, retrieving system configuration, and compressing files—all without writing additional code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python CLI Tricks: Using -m Modules, Port Testing, Simple Web Server, JSON Formatting, Text Editor, Executable Packaging, Encoding/Decoding, System Info, and File Compression

We start with the Python CLI and the python --help command to list available options, highlighting the -m mod flag which runs a module as a script.

1. Service Port Testing – Instead of installing telnet, Python’s telnetlib module can test outbound ports. Example: python -m telnetlib -d 142.250.70.174 443 shows a successful connection, while using an invalid port (e.g., 999) raises an error.

2. Launching a Local Web Server – A quick static server can be started with python -m http.server , which listens on port 8000 and serves files from the current directory, useful for sharing local resources.

3. Validating and Formatting JSON Strings – Long, unformatted JSON can be prettified using the built‑in json.tool module: echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}' | python -m json.tool . Adding a missing comma fixes the JSON and produces nicely indented output.

4. Creating a Simple Text Editor – Using the idlelib module (Tkinter‑based), a minimal UI editor can be launched: mkdir get_time_app && python -m idlelib get_time_app/print_time.py . The editor supports syntax highlighting and can be saved with Ctrl+S .

5. Building an Executable Application – Python’s zipapp module packages a directory into a single .pyz file: python -m zipapp get_time_app -m "print_time:main" . The resulting file runs via python get_time_app.pyz .

6. Encoding and Decoding Strings or Files – Demonstrations include ROT13 encryption with encodings.rot_13 , base64 encoding/decoding ( echo "I am Chris" | python -m base64 and echo "SSBhbSBDaHJpcwo=" | python -m base64 -d ), and encoding a script file for later execution.

7. Retrieving System Metadata – The python -m sysconfig command displays full Python configuration; python -m site shows environment paths and the current working directory.

8. File Compression – Python can create and extract zip archives without external tools: python -m zipfile -c get_time_app.zip get_time_app to compress, and python -m zipfile -e get_time_app.zip get_time_app_extracted to extract, followed by verification of extracted files.

CLIcommand linescriptingutilities
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.