几个 Python 通用命令行工具。
| 命令 | 用途 | |
|---|---|---|
| python -m http.server | 启动一个简单的web服务器 | |
| python -m webbrowser | 打开你的网页浏览器 | |
| python -m json.tool | 格式化JSON数据,使其美观 | |
| python -m calendar | 显示命令行日历 |
快速启动一个Web服务
http.server 将http.server模块作为脚本运行将启动一个在8000端口上的web服务器,它托管当前目录中的文件。
$ python -m http.server 在0.0.0.0端口8000上提供HTTP服务 (http://0.0.0.0:8000/) ...
webbrowser 将webbrowser模块作为脚本运行将打开默认网页浏览器中的给定URL。
例如,这将打开页面 test.com
$ python -m webbrowser pseudorandom.name
json.tool Python的json.tool模块可以作为脚本运行,解析JSON文档并打印出一个格式化得当、易于人类阅读的版本。
$ python -m json.tool /home/trey/Downloads/download.json
[
{
"title": "Python的walrus操作符",
"is_premium": false,
"url": "/using-walrus-operator/"
},
{
"title": "重构长布尔表达式",
"is_premium": true,
"url": "/refactoring-boolean-expressions/"
}
]
calendar 将calendar模块作为脚本运行默认会打印出当前年份的日历。它也接受各种参数来自定义其输出。下面是仅一个月的日历:
python3 -m calendar 2024 07
July 2024
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Have fun !