Python 冷兵器合集
點(diǎn)擊上方Python知識(shí)圈,設(shè)為星標(biāo)
回復(fù)100獲取100題PDF
閱讀文本大概需要 5 分鐘
python自帶了一些命令行工具,可以用來快速處理工作。我把這些命令行工具稱之為 冷兵器 ,沒有趁手工具時(shí)候可以頂替使用。這些工具都是python標(biāo)準(zhǔn)模塊,具有 main 函數(shù),直接使用 python -m 命令執(zhí)行,多數(shù)可以使用 -h/--help查看幫助。
1. http 服務(wù)
啟動(dòng)一個(gè)http靜態(tài)文件服務(wù)
# python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
2. json 格式化
格式化json數(shù)據(jù)
# echo '{"amount":3.4}' | python -m json.tool
{
"amount": 3.4
}
3. 可視化編輯器和shell
idlelib模塊基于tkinter,可以作為編輯器和shell使用。文件編輯器效果。
python -m idlelib myapp/example.py

shell效果
python -m idlelib

4. python 應(yīng)用程序打包
構(gòu)建myapp目錄如下:
├── myapp
│ ├── example.py
│ └── hello.py
代碼分別如下:
# example.py
import hello
def main():
print('Hello World')
hello.say_hello("python")
if __name__=='__main__':
main()
# hello.py
def say_hello(name):
print("hello",name)
將整個(gè)myapp打包成應(yīng)用程序,命令執(zhí)行后會(huì)生成一個(gè)名叫 myapp.pyz 應(yīng)用程序。
python -m zipapp myapp -m "example:main"
使用python直接運(yùn)行應(yīng)用程序
# python myapp.pyz
Hello World
hello python
5. ROT13 加密
rot13(rotate by 13 places)是一種簡易的替換式密碼,是凱撒密碼的變種。rot13將原文偏移13位形成密文,因?yàn)橛⑽目偣?6位,所以密文再偏移13位后會(huì)回到原文。公式: rot13(rot13(xxx))=xxx。
# echo "xxx" | python -m encodings.rot_13
Tvir zr n fgne
作為一個(gè)小彩蛋,歡迎大家動(dòng)手破譯一下xxx的值
6. base64 編碼
對(duì)一個(gè)字符串進(jìn)行base64編碼
# echo "haha" | python -m base64
aGFoYQo=
# echo "aGFoYQo=" | python -m base64 -d
haha
base64還支持對(duì)文件編碼。編寫測試代碼
# sample.py
def main():
print('Hello World??')
if __name__=='__main__':
main()
將代碼編譯成base64字符串
# python -m base64 sample.py
CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=
執(zhí)行編譯后的代碼
# echo "CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=" | python -m base64 -d | python
Hello World??
類似的可以使用uu(Unix-to-Unix encoding)編碼代碼:
# python -m uu sample.py
begin 644 sample.py
M"F1E9B!M86EN*"DZ"B @('!R:6YT*"=(96QL;R!7;W)L9/"?D8PG*0H@(" *
C:68@7U]N86UE7U\]/2=?7VUA:6Y?7R<Z"B @(&UA:6XH*0H
end
使用quopri(Encode and decode MIME quoted-printable data)編碼代碼:
# python -m quopri -t sample.py
def=20main():
=20=20=20print('Hello=20World=F0=9F=91=8C')
=20=20=20
if=20__name__=3D=3D'__main__':
=20=20=20main()
7. mime 識(shí)別
識(shí)別文件或者url的mime類型
# python -m mimetypes https://docs.python.org/3/library/mimetypes.html
type: text/html encoding: None # html
# python -m mimetypes https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png
type: image/png encoding: None # png
# python -m mimetypes sample.py
type: text/x-python encoding: None # python文件
# python -m mimetypes sample.py.gz
type: text/x-python encoding: gzip # python文件,gzip壓縮
8. 查看python環(huán)境信息
python -m sysconfig
Platform: "macosx-10.9-x86_64"
Python version: "3.8"
Current installation scheme: "posix_prefix"
Paths:
data = "/Users/yoo/work/yuanmahui/python/.venv"
include = "/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8"
...
Variables:
...
PYTHON = "python"
PYTHONFRAMEWORK = "Python"
PYTHONFRAMEWORKDIR = "Python.framework"
PYTHONFRAMEWORKINSTALLDIR = "/Library/Frameworks/Python.framework"
...
也可以使用下面命令查看系統(tǒng)路徑
# python -m site
sys.path = [
'/Users/yoo/work/yuanmahui/python/python-tools',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/yoo/work/yuanmahui/python/.venv/lib/python3.8/site-packages',
]
USER_BASE: '/Users/yoo/Library/Python/3.8' (exists)
USER_SITE: '/Users/yoo/Library/Python/3.8/lib/python/site-packages' (exists)
ENABLE_USER_SITE: False
9. 編譯腳本
compileall可以編譯python腳本。myapp目錄有2個(gè)py腳本
# ll myapp
total 16
-rw-r--r-- 1 yoo staff 118B 2 26 23:03 example.py
-rw-r--r-- 1 yoo staff 43B 2 26 23:03 hello.py
編譯腳本
# python -m compileall myapp
Listing 'myapp'...
Compiling 'myapp/example.py'...
Compiling 'myapp/hello.py'...
查看編譯結(jié)果
tree myapp -L 3
myapp
├── __pycache__
│ ├── example.cpython-38.pyc
│ └── hello.cpython-38.pyc
├── example.py
└── hello.py
1 directory, 4 files
10. 壓縮和解壓
創(chuàng)建和解壓tar包
# python -m tarfile -c myapp.tar myapp # 創(chuàng)建myapp.tar 壓縮包
# python -m tarfile -e myapp.tar myapp2 # 解壓myapp.tar 到 myapp2目錄
使用gzip壓縮文件
# python -m gizp sample.py
# python -m gzip -d sample.py.gz
使用zip打包文件
# python -m zipfile -c myapp.zip myapp
# python -m zipfile -e myapp.zip myapp2
注意: zipfile和zipapp不一樣, 后則是生成一個(gè)可以執(zhí)行的app
11. telnet 工具
沒有telnet工具的python容器中可以這樣:
# python -m telnetlib -d redis 6379 # 連接redis
monitor
Telnet(redis,6379): send b'monitor\n'
Telnet(redis,6379): recv b'-NOAUTH Authentication required.\r\n'
-NOAUTH Authentication required.
還有 nntplib && ftplib 兩個(gè)工具,應(yīng)該很少使用,就不介紹了
12. 性能和調(diào)試工具
自帶的timeit可以測試腳本的性能數(shù)據(jù)
# python -m timeit '"-".join([str(n) for n in range(100) if n%2 == 0])' # 取模求偶數(shù)
20000 loops, best of 5: 12.5 usec per loop
# python -m timeit '"-".join([str(n) for n in range(0,100,2)])' # 步進(jìn)
50000 loops, best of 5: 8.85 usec per loop
# python -m timeit '"-".join([str(n) for n in range(100) if n&1 == 0])' # 位運(yùn)算判斷奇偶
20000 loops, best of 5: 14.3 usec per loop
不科學(xué)1: 位運(yùn)算竟然比取模慢?
如果不是一個(gè)字符串而是一個(gè)腳本:
def test_normal():
tmp = ""
for x in range(100):
if x % 2 == 0:
if tmp:
tmp = tmp+"-"+str(x)
else:
tmp = str(x)
return tmp
if __name__ == '__main__':
print(test_normal())
可以下面方式變通使用
# python -m base64 test_string_join.py | python -m base64 -d | python -m timeit
50000000 loops, best of 5: 5.33 nsec per loop
不科學(xué)2: 50000000循環(huán)只需要5.33納秒,而之前的用例20000循環(huán)需要12.5微秒
標(biāo)準(zhǔn)庫中還有 pdb && profile && pstats 使用比較復(fù)雜,暫時(shí)就不介紹
pypy3 -m timeit '[{} for n in range(1000)]'
WARNING: timeit is a very unreliable tool. use pyperf or something else for real measurements
pypy3 -m pip install pyperf
pypy3 -m pyperf timeit '[{} for n in range(1000)]'
------------------------------------------------------------
100000 loops, average of 7: 7.3 +- 0.107 usec per loop (using standard deviation)
[tywork@liujunhong_szfsfz_work1 config]$ pypy3 -m pyperf timeit '[{} for n in range(1000)]'
........
Mean +- std dev: 8.42 us +- 0.25 us
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$ pypy3 -m pyperf timeit '[dict() for n in range(1000)]'
.........
Mean +- std dev: 29.6 us +- 1.1 us
13. pydoc
本地服務(wù)方式查看python代碼文檔
# python -m pydoc -p 8080 # 啟動(dòng)一個(gè)web服務(wù)
Server ready at http://localhost:8080/
Server commands: [b]rowser, [q]uit
server> b
文檔效果如圖

14. test
執(zhí)行python自帶的測試用例,查看系統(tǒng)支持那些特性
# python -m test.regrtest -u cpu
== CPython 2.7.16 (default, Jun 5 2020, 22:59:21) [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc-
== Darwin-19.6.0-x86_64-i386-64bit little-endian
== /private/var/folders/mv/3vgd3mdx2453clfcst7qlm280000gn/T/test_python_13470
== CPU count: 12
Run tests sequentially
0:00:00 load avg: 2.53 [ 1/404] test_grammar
...
= Tests result: FAILURE ==
363 tests OK.
5 tests failed:
test_import test_posix test_py_compile test_rlcompleter
test_scriptpackages
36 tests skipped:
test_al test_bsddb test_bsddb3 test_cd test_cl test_codecmaps_cn
test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr
test_codecmaps_tw test_curses test_epoll test_gdb test_gdbm
test_gl test_imgfile test_largefile test_linuxaudiodev test_msilib
test_ossaudiodev test_poll test_py3kwarn test_smtpnet
test_socketserver test_spwd test_startfile test_sunaudiodev
test_timeout test_tk test_tools test_ttk_guionly test_urllib2net
test_urllibnet test_winreg test_winsound test_zipfile64
2 skips unexpected on darwin:
test_spwd test_tools
Total duration: 5 min 23 sec
Tests result: FAILURE
從測試用例,可以看到osx支持fork,不支持epool和poll。
0:00:47 load avg: 1.79 [138/404] test_fork1
...
0:00:39 load avg: 1.59 [125/404] test_epoll
test_epoll skipped -- test works only on Linux 2.6
...
0:02:42 load avg: 2.41 [257/404/1] test_poll
test_poll skipped -- select.poll not defined -- skipping test_poll
0:02:42 load avg: 2.41 [258/404/1] test_popen -- test_poll skipped
參考鏈接
PEP441--Improving Python ZIP Application Support https://www.python.org/dev/peps/pep-0441/ ROT13 https://zh.wikipedia.org/wiki/ROT13
往期推薦 01 02 03
↓點(diǎn)擊閱讀原文查看pk哥原創(chuàng)視頻
我就知道你“在看” 

