several interesting Python command line tools

created at 07-08-2021 views: 5

1. http service

Start an http static file service with http module

# python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

2. json format

Format json data

# echo '{"amount":3.4}' | python -m json.tool
{
    "amount": 3.4
}

3. Visual editor and shell

The idlelib module is based on tkinter and can be used as an editor and shell. File editor effect.

python -m idlelib myapp/example.py

result

python -m idlelib

shell:

shell

4. Python application packaging

Build the myapp directory as follows:

├── myapp
│   ├── example.py
│   └── hello.py

The codes are as follows:

# 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)

Package the entire myapp into an application. After the command is executed, an application named myapp.pyz will be generated.

python -m zipapp myapp -m "example:main"

Use python to run the application directly

# python myapp.pyz
Hello World
hello python

5. ROT13 encryption

Rot13 (rotate by 13 places) is a simple replacement password, a variant of the Caesar password. rot13 shifts the original text by 13 bits to form a ciphertext. Because the English has 26 bits in total, the ciphertext will return to the original text after shifting 13 bits. Formula: rot13(rot13(xxx))=xxx.

# echo "xxx" | python -m encodings.rot_13
Tvir zr n fgne

6. base64 encoding

Base64 encode a string

# echo "haha" | python -m base64
aGFoYQo=
# echo "aGFoYQo=" | python -m base64 -d
haha

base64 also supports file encoding. Write test code

# sample.py

def main():
   print('Hello World')

if __name__=='__main__':
   main()

Compile the code into a base64 string

# python -m base64 sample.py
CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=

Execute the compiled code

# echo "CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=" | python -m base64 -d | python
Hello World

Similarly, uu (Unix-to-Unix encoding) encoding code can be used:

# 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

Use quopri (Encode and decode MIME quoted-printable data) encoding code:

# 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 recognition

Identify the mime type of a file or url

# 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 file
# python -m mimetypes sample.py.gz
type: text/x-python encoding: gzip  # python file,gzip compression

8. Check python environment information

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"
 ...

You can also use the following command to view the system path

# 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. Compile the script

compileall can compile python scripts. There are 2 py scripts in the myapp directory

# 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

Compile script

# python -m compileall myapp
Listing 'myapp'...
Compiling 'myapp/example.py'...
Compiling 'myapp/hello.py'...

Check compilation results

tree myapp -L 3
myapp
├── __pycache__
│   ├── example.cpython-38.pyc
│   └── hello.cpython-38.pyc
├── example.py
└── hello.py

1 directory, 4 files

10. Compression and decompression

Create and unzip the tar package

# python -m tarfile -c myapp.tar myapp # Create myapp.tar compressed package
# python -m tarfile -e myapp.tar myapp2 # Unzip myapp.tar to the myapp2 directory

Use gzip to compress files

# python -m gizp sample.py
# python -m gzip -d sample.py.gz

Use zip to package files

# python -m zipfile -c myapp.zip myapp
# python -m zipfile -e myapp.zip myapp2

Note: zipfile and zipapp are not the same, after that, an executable app is generated

11. telnet tool

This can be done in a python container without the telnet tool:

# python -m telnetlib -d redis 6379  # connect redis
monitor
Telnet(redis,6379): send b'monitor\n'
Telnet(redis,6379): recv b'-NOAUTH Authentication required.\r\n'
-NOAUTH Authentication required.

12. Performance and debugging tools

The built-in timeit can test the performance data of the script

# python -m timeit '"-".join([str(n) for n in range(100) if n%2 == 0])'  # Modulo and even
20000 loops, best of 5: 12.5 usec per loop
# python -m timeit '"-".join([str(n) for n in range(0,100,2)])'  # Stepping
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])'  # Bit arithmetic to determine parity
20000 loops, best of 5: 14.3 usec per loop

If it is not a string but a script:

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())

Can be used in the following ways

# python -m base64 test_string_join.py | python -m base64 -d | python -m timeit
50000000 loops, best of 5: 5.33 nsec per loop

There is also pdb && profile && pstats in the standard library, which is more complicated to use, so I won’t introduce it for now.

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

View python code documentation in local service mode

# python -m pydoc -p 8080  # start a web server
Server ready at http://localhost:8080/
Server commands: [b]rowser, [q]uit
server> b

python doc

14. test

Execute the test cases that come with python to see which features the system supports

# 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

From the test case, we can see that osx supports fork, but does not support epool and 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
Please log in to leave a comment.