相信做 Python 的都知道 Python 有一个内置的 HTTP Server,可能经常临时性使用一下。
今天,我在 https://til.simonwillison.net/python/stdlib-cli-tools 上了解到 Python 其实内置了不少小工具,可以了解一下,说不定可以用到。
HTTP 服务器
python -m http.server
Telnet 客户端
python -m telnetlib smtp.sendcloud.net 25
NNTP(网络新闻传输协议)
https://en.wikipedia.org/wiki/Network_News_Transfer_Protocol
python -m nntplib
Group gmane.comp.python.general has 757312 articles, range 23546 to 848666
848657 dn via Python-list Re: isinstance() (64)
848658 Chris Angelico v... Re: isinstance() (57)
848659 dn via Python-list Re: isinstance() (84)
848660 Chris Angelico v... Re: isinstance() (11)
848661 Grant Edwards vi... Re: isinstance() (34)
848662 Thomas Wouters v... [RELEASE] Python 3.12.0 release candid... (117)
848663 "Peter J. Holzer... Where is the error? (94)
848664 dn via Python-list Re: Where is the error? (58)
848665 Cameron Simpson ... Re: Where is the error? (43)
848666 Barry via Python... Re: Where is the error? (17)
日历
python -m calendar
json.tool
python -m json.tool --help
usage: python -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact] [infile] [outfile]
A simple command line interface for json module to validate and pretty-print JSON objects.
positional arguments:
infile a JSON file to be validated or pretty-printed
outfile write the output of infile to outfile
options:
-h, --help show this help message and exit
--sort-keys sort the output of dictionaries alphabetically by key
--no-ensure-ascii disable escaping of non-ASCII characters
--json-lines parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid JSON Lines output.
--indent INDENT separate items with newlines and use this number of spaces for indentation
--tab separate items with newlines and use tabs for indentation
--no-indent separate items with spaces rather than newlines
--compact suppress all whitespace separation (most compact)
echo '{"foo": "bar", "baz": [1, 2, 3]}' | python -m json.tool
{
"foo": "bar",
"baz": [
1,
2,
3
]
}
GZip 压缩
python -m gzip --help
usage: gzip.py [-h] [--fast | --best | -d] [file ...]
A simple command line interface for the gzip module: act like gzip, but do not delete the input file.
positional arguments:
file
options:
-h, --help show this help message and exit
--fast compress faster
--best compress better
-d, --decompress act like gunzip instead of gzip
Base64
python -m base64 --help
option -h not recognized
usage: /usr/lib/python3.10/base64.py [-d|-e|-u|-t] [file|-]
-d, -u: decode
-e: encode (default)
-t: encode and decode string 'Aladdin:open sesame'
UU 编码
Python 3.13 中移除。
python -m uu --help
Usage: uu.py [-d] [-t] [input [output]]
Options:
-h, --help show this help message and exit
-d, --decode Decode (instead of encode)?
-t, --text data is text, encoded format unix-compatible text?
AsyncIO REPL
python -m asyncio
asyncio REPL 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>>
Python 语法相关
python -m tokenize --help
usage: python -m tokenize [-h] [-e] [filename.py]
positional arguments:
filename.py the file to tokenize; defaults to stdin
options:
-h, --help show this help message and exit
-e, --exact display token names using the exact type
python -m ast --help
usage: python -m ast [-h] [-m {exec,single,eval,func_type}] [--no-type-comments] [-a] [-i INDENT] [infile]
positional arguments:
infile the file to parse; defaults to stdin
options:
-h, --help show this help message and exit
-m {exec,single,eval,func_type}, --mode {exec,single,eval,func_type}
specify what kind of code must be parsed
--no-type-comments don't add information about type comments
-a, --include-attributes
include attributes such as line numbers and column offsets
-i INDENT, --indent INDENT
indentation of nodes (number of spaces)
其他
可以探索探索,找到更多小工具:
cd /usr/lib/python3.10/ && grep -Fral 'if **name** =' . | grep -ve 'test/' -e 'tests/'