The specific contents are as follows:
1 Os.system
For example, run the following command in Ipython to return to the status
Os.system (' cat/etc/passwdqc.conf ')
min=disabled,24,11,8,7
Max=40
Passphrase=3
Match=4
Similar=deny
random=47
Enforce=everyone
Retry=3
OUT[6]: 0
2 Os.popen ()
Popen (command [, mode= ' R ' [, BufSize]]), pipe
Open a pipe to/from a command returning a file object.
Run the return result
In []: output = Os.popen (' Cat/proc/cpuinfo ')
in [+]: Linelen = []
in [+]: for line in Output.readlines ():
Linelen.append (len (line))
....:
In [All]: line
Line Linelen
In [all]: Linelen
OUT[23]:
[14,
25,
...
3 How to return the results and running status at the same time, commands module:
#String form:
File:/usr/lib64/python2.7/commands.pydocstring:execute shell commands via Os.popen () and return status, output. Interface Summary:import commandsouttext = commands.getoutput (cmd) (exitstatus, Outtext) = Commands.getstatusoutput ( CMD) Outtext = commands.getstatus (file) # returns output of "Ls-ld file" A trailing newline is removed from the output stri Ng. Encapsulates the basic operation:pipe = Os.popen (' {' + cmd + ';} 2>&1 ', ' r ') Text = Pipe.read () sts = Pipe.close () c3/>
The commands example is as follows:
in []: (status, Output) = Commands.getstatusoutput (' Cat/proc/cpuinfo ')
in [+]: status
OUT[25]: 0
in [+]: Len (output)
OUT[26]: 3859
4 using the module subprocess
Run "? subprocess" in Ipython to discover that subprocess is a new module that Python uses to replace a pipeline operation command such as Os.popen ()
A More Real-world example would look like this:
Try: Retcode = Call ("myCMD" + "Myarg", shell=true) if Retcode < 0: print >>sys.stderr, "Child is Te Rminated by Signal ",-retcode else: print >>sys.stderr," Child returned ", Retcodeexcept OSError, E: Print >>sys.stderr, "Execution failed:", E
Subprocess facilitates the control and monitoring of process results relative to the above, Subprocess provides a variety of functions to handle the different requirements of parent process sub-processes:
4.1.1 Subprocess.call ()
Parent process parent process waits for child process to complete, returns exit code
4.1.2 Subprocess.check_call ()
The parent process waits for the child process to complete, returns 0, and if ReturnCode is not 0, enumerates error subprocess. Calledprocesserror, this object contains the ReturnCode property, which can be try...except ... To check
4.1.3 Subprocess.check_output ()
Parent process waits for child process to complete
Returns the output of the child process to the standard output
Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, which contains the ReturnCode property and the output property, outputs a result of the standard output, available try...except ... To check
For example:
in [+]: out = Subprocess.call ("Ls-l", Shell=true)
Total 42244
-rw-rw-r--. 1 * * * * 366 may 09:10 ChangeLog
4.2.1
The above three functions are derived from the Wapper (encapsulation) of the Popen () function, and if more personalization is needed, the Popen () function is required
After the Popen object is created, the main program does not automatically wait for the child process to complete. We must call the object's Wait () method before the parent process waits (that is, block block)
[Wenwt@localhost syntax]$ rm subprocess.pyc [wenwt@localhost syntax]$ python process.py parent Process[wenwt@localhost syntax]$ Ping www.google.com (173.194.219.99) (+) bytes of data.^c[wenwt@localhost syntax]$---www.google.com ping sta Tistics---5 packets transmitted, 0 received, 100% packet loss, time 3999ms
Add the Wait method:
[Wenwt@localhost syntax]$ python process.py PING www.google.com (173.194.219.103) bytes of data.---www.google.com Ping Statistics---5 packets transmitted, 0 received, 100% packet loss, time 3999msparent process
The above content is the entire narrative of this article, I hope you like.