Knowledge points of shell execution in python

os.system

The system method creates a child process to run an external program, and the method only returns the results of the external program. This method is more suitable for situations where the external program has no output results.

import os
os.system('ls')

commands.getstatusoutput

Use the getoutput method of the commands module. The difference between this method and popend is that popen returns a file handle, while this method returns the output result of an external program as a string, which is more convenient in many cases.

Main method:

This method is very useful when you need to get the output result of an external program. For example, when using urllib to call Web API, you need to process the obtained data. os.popen(cmd) To get the output of the command, just call read() or readlines() again, such as a=os.popen(cmd).read()

import os
ls = os.popen('ls')
print ls.read()

commands.getstatusoutput

Use the getoutput method of the commands module. The difference between this method and popend is that popen returns a file handle, while this method returns the output result of an external program as a string, which is more convenient in many cases.
Main method:

import commands
commands.getstatusoutput('ls -lt')   #return(status, output)

subprocess.call

According to the official Python documentation, the subprocess module is used to replace these modules. There is a parallel ssh tool implemented in Python-mssh, the code is very short, but very interesting, it calls subprocess in the thread to start the child process to work.

from subprocess import call
call(["ls","-l"])
import shlex, subprocess
def shell_command(cmd, timeout):
 data ={"rc":False,"timeout":False,"stdout":"","stderr":""}try:
 process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)try:
  outs, errs = process.communicate(timeout=timeout)
  data["stdout"]= outs.decode("utf-8") 
  data["stderr"]= errs.decode("utf-8") 
  data["rc"]= True

 except subprocess.TimeoutExpired :
  process.kill()
  outs, errs = process.communicate()
  data["rc"]= False 
  data["stdout"]= outs.decode("utf-8") 
  data["stderr"]="timeout"
  data["timeout"]= True 

 except Exception as e :
 data["rc"]= False 
 data["stderr"]= e 

 finally:return data 

So far this article on the knowledge points of shell execution in python is introduced. For more related python shell execution content, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!

Recommended Posts

Knowledge points of shell execution in python
Python knowledge points
Python3 supplementary knowledge points
Python advanced knowledge points
Basic knowledge of Python (1)
Subscripts of tuples in Python
2020--Python grammar common knowledge points
Noteworthy update points in Python 3.9
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Python crawler basic knowledge points finishing
Use of numpy in Python development
Detailed usage of dictionary in Python
Usage of os package in python
The usage of tuples in python
Description of in parameterization in python mysql
Understanding the meaning of rb in python
Python implements alternate execution of two threads
Implementation of JWT user authentication in python
Analysis of glob in python standard library
Method of installing django module in python
What is the function of adb in python
Summarize the knowledge points of Centos7 system reinforcement
Functions in python
7 features of Python3.9
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
The consequences of uninstalling python in ubuntu, very
Installation and use of GDAL in Python under Ubuntu
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
03. Operators in Python entry
12. Network Programming in Python3
print statement in python
Basics of Python syntax
Concurrent requests in Python
Basic syntax of Python
Install python in Ubuntu
Prettytable module of python
Write gui in python
MongoDB usage in Python
09. Common modules of Python3
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
A large inventory of commonly used third-party libraries in Python