14 Python command line cold weapons, one more powerful than the other!

Posted by phpnoobie on Sat, 18 Dec 2021 08:38:27 +0100

Python comes with some command-line tools that can be used to quickly process work. I call these command-line tools cold weapons. They can be used instead when they are not available. These tools are Python standard modules with main function, which can be executed directly with python -m command. Most of them can use - H / - help to view help.

1. http service

Start an http static file service

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

2. json formatting

Formatting 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 effects.

python -m idlelib myapp/example.py

shell effect

python -m idlelib

gui-shell

4. python application packaging

Build 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 whole myapp into an application. After the command is executed, a new one named myapp will be generated Pyz application.

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

Running applications directly using python

# python myapp.pyz
Hello World
hello python

5. ROT13 encryption

Rot13 (rotate by 13 places) is a simple substitution cipher, which is a variant of Caesar's cipher. Rot13 offsets the original text by 13 bits to form a ciphertext. Because English has a total of 26 bits, the ciphertext will return to the original text after offsetting another 13 bits. Formula: rot13(rot13(xxx))=xxx.

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

As a small colored egg, you are welcome to decipher the value of xxx

6. base64 coding

base64 encodes a string

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

base64 also supports encoding files. 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 compiled code

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

Similarly, UU (UNIX to UNIX encoding) can be used to encode the code:

# 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) to encode the 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 identification

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

8. View 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 view the system path using the following command

# 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. Compiling scripts

compileall can compile python scripts. There are two 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'...

View 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 myapp2 directory

Compress files using gzip

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

Package files using zip

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

Note: zipfile is different from zipapp, and then an executable app is generated

11. telnet tools

In python containers without telnet tools, you can do this:

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

There are also two tools, nntplib & & ftplib, which should be rarely used. I won't introduce them

12. Performance and commissioning 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 even
20000 loops, best of 5: 12.5 usec per loop
# python -m timeit '"-".join([str(n) for n in range(0,100,2)])'  #Step
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 operation judgment parity
20000 loops, best of 5: 14.3 usec per loop

Unscientific 1: bit operation is slower than modulo?

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

It can be used flexibly 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

Unscientific 2: 50 million cycles require only 5.33 nanoseconds, while the previous use case 20000 cycles require 12.5 microseconds

There are PDB & & profile & & pstats in the standard library, which is complex to use, so I won't introduce it for the time being

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

Viewing python code documents in local service mode

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

The document effect is shown in the figure below

14. test

Execute the test cases provided 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 cases, we can see that osx supports fork and 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

Topics: Python