示例代码如下

import subprocess
def execute_shell_command(command):
    res = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8',
                         timeout=180,
                         executable='/bin/bash')
    if res.returncode == 0:
        print('return code ===> ', res.returncode)
        print(res.stdout)
    else:
        print('return code ===> ', res.returncode)
        print(res.stderr)


print('<' + '=' * 50 + ' 执行正确的shell命令 ' + '=' * 50 + '>')
execute_shell_command('ls -lath')
print('<' + '=' * 50 + ' 执行错误的shell命令 ' + '=' * 50 + '>')
execute_shell_command('apt list111')
print('<' + '=' * 50 + ' 查看安装的Python包 ' + '=' * 50 + '>')
execute_shell_command('pip3 list')
print('<' + '=' * 50 + ' 安装Python包 ' + '=' * 50 + '>')
execute_shell_command('pip3 install -i https://pypi.douban.com/simple django')

执行结果

<================================================== 执行正确的shell命令 ==================================================>
return code ===>  0
total 24K
drwxrwxr-x  3 w w 4.0K 4月  22 13:29 .
-rw-rw-r--  1 w w 1.4K 4月  22 13:29 aaa.py
drwxrwxr-x 12 w w 4.0K 4月  15 19:00 ..
-rw-rw-r--  1 w w  624 4月  15 13:33 bbb.py
drwxrwxr-x  2 w w 4.0K 4月  14 22:50 __pycache__
-rw-rw-r--  1 w w  147 4月  14 22:46 __init__.py

<================================================== 执行错误的shell命令 ==================================================>
return code ===>  100

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

E: 无效的操作 list111

<================================================== 查看安装的Python包 ==================================================>
return code ===>  0
Package           Version
----------------- ---------
appdirs           1.4.4
attrs             20.3.0
black             19.10b0
certifi           2020.12.5
chardet           3.0.4
click             7.1.2
ConfigArgParse    1.4
fastapi           0.63.0
Flask             1.1.2
Flask-BasicAuth   0.2.0
gevent            21.1.2
geventhttpclient  1.4.5
greenlet          1.0.0
httprunner        3.1.4
idna              2.10
itsdangerous      1.1.0
Jinja2            2.11.3
jmespath          0.9.5
locust            1.4.3
loguru            0.4.1
MarkupSafe        1.1.1
more-itertools    8.7.0
msgpack           1.0.2
packaging         20.9
pathspec          0.8.1
pip               21.0.1
pkg-resources     0.0.0
pluggy            0.13.1
prettytable       0.7.2
psutil            5.8.0
py                1.10.0
pydantic          1.8.1
pyparsing         2.4.7
pytest            5.4.3
pytest-html       2.1.1
pytest-metadata   1.11.0
PyYAML            5.4.1
pyzmq             22.0.3
regex             2021.3.17
requests          2.23.0
sentry-sdk        0.14.4
setuptools        44.0.0
simplejson        3.17.0
six               1.15.0
starlette         0.13.6
tinydb            3.15.1
toml              0.10.2
typed-ast         1.4.2
typing-extensions 3.7.4.3
urllib3           1.25.11
wcwidth           0.2.5
Werkzeug          1.0.1
whatportis        0.8.1
zope.event        4.5.0
zope.interface    5.2.0

<================================================== 安装Python包 ==================================================>
return code ===>  0
Looking in indexes: https://pypi.douban.com/simple
Collecting django
  Downloading https://pypi.doubanio.com/packages/a8/9b/fe94c509e514f6c227308e81076506eb9d67f2bfb8061ce5cdfbde0432e3/Django-3.2-py3-none-any.whl (7.9 MB)
Collecting asgiref<4,>=3.3.2
  Downloading https://pypi.doubanio.com/packages/17/8b/05e225d11154b8f5358e6a6d277679c9741ec0339d1e451c9cef687a9170/asgiref-3.3.4-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
  Downloading https://pypi.doubanio.com/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting pytz
  Downloading https://pypi.doubanio.com/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl (510 kB)
Installing collected packages: sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.3.4 django-3.2 pytz-2021.1 sqlparse-0.4.1


Process finished with exit code 0

Q.E.D.


一只鹅啊