Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python executes external commands

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how Python executes external commands". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how Python executes external commands" can help you solve the problem.

First, understand subprocess

Subeprocess module is a module that comes with python and does not need to be installed. It is mainly used to replace some modules or methods, such as os.system, os.spawn*, os.popen, commands.* and so on.

Therefore, executing external commands gives priority to using the subprocess module.

1. Subprocess.run () method

The subprocess.run () method is officially recommended, and almost all work can be done with it.

The following is the function source code:

Subprocess.run (args, *, stdin=None,input=None,stdout=None,stderr=None,shell=False,ced=None,timeout=None,check=False,enccoding=None,error=None)

This function returns an instance of the CompletedProcess class (with parameters passed in by attributes and return values). This function has many parameters, so you only need to remember the commonly used ones.

1. Args: represents the command that needs to be executed in the operating system, either in the form of a string (shell=True is required) or a list list type

2. *: represents a variable parameter, usually a list or dictionary type

3. Stdin, stdout, stderr: specify the standard input, standard output and standard error file handle of the executable program

4, shell: represents whether the program needs to be executed on shell. When you want to use the features of shell, set shell=True so that you can use the pipes of shell instructions, file name wildcards, environment variables, etc., but python also provides many modules similar to shell, such as glob, fnmatch, os.walk (), os.path.expandvars (), os.path.expanduser () and shutil.

5. Check: if check is set to True, check the return value of the command. When the return value is 0, an exception of AclledProcessError will be thrown

6. Timeout: set the timeout. If the timeout occurs, kill drops the child process.

1. Execute the shell command using a string

[root@localhost python] # vim 1.PYRAMBING Env python3import subprocessb=subprocess.run ("ls-l / ltp | head-2", shell=True) # executes the run method and assigns the return value to b # total 18498 bind-rw-r--r--. 1 root root 10865 May 8 16:21 123.txtprint (b) # the CompletedProcess class returned after executing the run function # CompletedProcess (args='ls-l / ltp | head-2 steps, returncode=0) print (b.args) # prints out the agrs attribute value of the CompletedProcess class, which is the shell command # ls-l / ltp | head-2print (b.returncode) # status code executed by the print command # 0

Result display

[root@localhost python] #. / 1.py

Total 184980

-rw-r--r--. 1 root root 10865 May 8 16:21 123.txt

CompletedProcess (args='ls-l / ltp | head-2percent, returncode=0) 2

Ls-l / ltp | head-2

0

two。 Execute using list mode

Personal feeling method 2 is not easy to use, especially when you want to use pipe symbols.

#! / bin/env python3import subprocessb = subprocess.run (["ls", "- l", "/ ltp"]) print (b) print (b.args) print (b.returncode)

Execution result

[root@localhost python] #. / 2.py

Total 10865

-rw-r--r--. 1 root root 10865 May 8 16:21 123.txt

CompletedProcess (args= ['ls','-lumped,'/ ltp'], returncode=0)

['ls','-lame,'/ ltp']

0

3. Capture script output

If you need to collect the results of command execution, you can pass the parameter stdout=subprocess.PIPE

[root@localhost python] # cat 3.PYBINGUBING Env python3import subprocess# input stdout=subprocess.PIPE parameter to b=subprocess.run ("ls-l / ltp | head-2", shell=True, stdout=subprocess.PIPE) print (b.stdout)

The results show that

[root@localhost python] #. / 1.py

B'total 184980-rw-r--r--. 1 root root 10865 May 8 16:21 123.txt'

4. Detect abnormality

Example 1: simulation renturncode value is not 0

Pass the parameter check=True. If the returned value is not 0, an exception will be thrown.

[root@localhost python] # cat 1.PYRAMBING Env python3import subprocessb=subprocess.run ("ls-l / 123 | head-2 & & exit 1", shell=True, stdout=subprocess.PIPE, check=True) print (b.returncode)

Execution result: CalledProcessError type error returned

[root@localhost python] #. / 1.pyls: cannot access / 123: No such file or directoryTraceback (most recent call last): File ". / 1.py", line 3, in b=subprocess.run ("ls-1 / 123 | head-2 & & exit 1", shell=True, stdout=subprocess.PIPE, check=True) File "/ usr/local/python3/lib/python3.7/subprocess.py", line 487, in run output=stdout Stderr=stderr) subprocess.CalledProcessError: Command'ls-l / 123 | head-2 & & exit 1 'returned non-zero exit status 1 # returned an error of CalledProcessError type

Example 2: simulation execution timeout

Return TimeoutExpired exception

[root@localhost python] # vim 1.PYRAMBING Env python3import subprocessb=subprocess.run ("while 2 > 1 print do sleep 1 done", timeout=3, shell=True, stdout=subprocess.PIPE, check=True) print (b.returncode)

Display the results

[root@localhost python] #. / 1.py

Traceback (most recent call last):

File "/ usr/local/python3/lib/python3.7/subprocess.py", line 474, in run

Stdout, stderr = process.communicate (input, timeout=timeout)

File "/ usr/local/python3/lib/python3.7/subprocess.py", line 939, in communicate

Stdout, stderr = self._communicate (input, endtime, timeout)

File "/ usr/local/python3/lib/python3.7/subprocess.py", line 1682, in _ communicate

Self._check_timeout (endtime, orig_timeout)

File "/ usr/local/python3/lib/python3.7/subprocess.py", line 982, in _ check_timeout

Raise TimeoutExpired (self.args, orig_timeout)

Subprocess.TimeoutExpired: Command 'while 2 > 1 * * do sleep 1 * * timed out after 3 seconds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File ". / 1.py", line 3, in

B=subprocess.run ("while 2 > 1 do sleep 1 done", timeout=3, shell=True, stdout=subprocess.PIPE, check=True)

File "/ usr/local/python3/lib/python3.7/subprocess.py", line 479, in run

Stderr=stderr)

Subprocess.TimeoutExpired: Command 'while 2 > 1 * * do sleep 1 * * timed out after 3 seconds

2. PIN class

1. A preliminary understanding of Popen class

First, take a look at the constructor of the Popen class

Class Popen (args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0):

Parameter string or list bufsize0: no buffering

Parameter string or list bufsize0: no buffering

1: row buffering

Other positive values: buffer size

Negative values: default system buffering (usually full buffering) executable is generally not used, the first item of the args string or list represents the program name stdin

Stdout

StderrNone: there is no redirect inheritance parent process

PIPE: creating pipe

File object

File descriptor (integer)

Stderr can also be set to the stdoutpreexec_fn hook function to determine whether to close files other than fork 1 and 2 before executing a new process under close_fdsunix between fork and exec.

Description of the file that does not inherit or inherit the parent process under windows. If shell is True:

Under unix, it is equivalent to adding "/ bin/bash"- c" before args.

Under windows, it is equivalent to adding "cmd.exe / c" cwd setting working directory env setting environment variable unviersal_newlines various newline characters are processed uniformly into the structure passed to CreateProcess under "" startupinfowindows, pass CREATE_NEW_CONSOLE to create your own console window.

How to use 2.Popen

1. Subprocess.Popen (["cat", "abc.txt"])

2. Subprocess.Popen ("cat abc.txt", shell=True)

The second one above is actually equivalent to: subprocess.Popen (["/ bin/bash", "- c", "cat abc.txt"])

Example:

[root@localhost python] # cat 3.PYBING Env python3import subprocessobj = subprocess.Popen ("ls-l / ltp", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Universal_newlines=True) error_info = obj.stderr.read () out_info = obj.stdout.read () result = out_info + error_infoprint (result) [root@localhost python] #. / 3.pytotal 184980 RWKui. 1 root root 10865 May 8 16:21 123.txtmuri rwmuri. 1 root root 802 Apr 21 09:42 ab.shdrwxr-xr-x. 3 root root 101 Apr 1 18:34 auth-rw-r--r--. 1 root root 5242880 Mar 18 13:20 bigfile-rwxrwxrwx. 1 root root 1392 Feb 5 09:24 dingding.sh

Object methods of the Popen class

The name function poll () checks whether it is finished, sets the return value wait () to wait for the end, sets the return value communicate () parameter is standard input, returns standard output and standard error send_signal () start signal (mainly useful under linux) terminate () terminates the process, unix corresponding SIGTERM signal, windows calls the api function TerminateProcess () kill () to kill the process (unix corresponds to SIGKILL signal), windows is the same as above stdin

Stdout

Useful pid process idreturncode process return value when PIPE is specified in the stderr parameter

Add: other methods

1. Subeprocess.call (* args,**kwargs): the call () method calls Popen () to execute the program and waits for it to complete

2. Subpeocess.check_call (* args, * * kwargs): call the above call (). If the returned value is non-zero, an exception is returned.

3. Subprocess.check_output (* args, * * kwargs): call Popen () to execute the program and return standard output

Supplementary os module executes external commands 1, os.system () method

Example:

[root@localhost python] # cat 4.PYRAMBING BINV python3import os# variable ret receives the return value ret = os.system ('ls-l / ltp | head-2') print ("execution succeeded" if ret = = 0 else "execution failed") after the command was executed.

Execution result

[root@localhost python] #. / 4.py

Total 184980

-rw-r--r--. 1 root root 10865 May 8 16:21 123.txt

Successful execution

2. Usage of os.popen ()

Similar to subprocess.Popen (), do not write

Add: the execution results of subprocess.run () and subprocess.Popen () are written to the cache, and the results can be printed after execution, but not output in real time at the terminal, while os.system () is output to the terminal interface in real time.

That's all for "how Python executes external commands". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report