[Python2.7] Summary of how to use subprocess

Overview

There are quite a lot of opportunities to use subprocess, so I will summarize the points I investigated without understanding. This article is for Python 2.7, as I assumed the Python 2.7 environment as my use case at the time of my research.

Basically, please refer to the official document below. 17.1. Subprocess — Subprocess Management — Python 2.7.x Documentation

Especially in the case of Python 3.5 or higher, the usage is different because the general-purpose run () command is added. Please refer to the formula without diversion as it is. 17.5. Subprocess — Subprocess Management — Python 3.5.2 Documentation

Import specification

import


import subprocess

Basic API

subprocess.call -Runtime standard output / standard error is output at the time of execution ・ Returns a return code in both normal and abnormal conditions

Execution sample


import subprocess

print '!!! start'

cmd = 'echo aaa'
retcode = subprocess.call(cmd.split())
print retcode

cmd = 'false'
retcode = subprocess.call(cmd.split())
print retcode

print '!!! end'

output


aaa
!!! start
0
1
!!! end

subprocess.check_call -Runtime standard output / standard error is output at the time of execution ・ Returns 0 return code when normal -In case of abnormality, subprocess.CalledProcessError exception is sent.

Execution sample


import subprocess

print '!!! start'

cmd = 'echo aaa'
retcode = subprocess.check_call(cmd.split())
print retcode

cmd = 'false'
try:
    retcode = subprocess.check_call(cmd.split())
except subprocess.CalledProcessError as e:
    print e.returncode
    print e.cmd
    print e.output

print '!!! end'

output


aaa
!!! start
0
1
['false']
None
!!! end

subprocess.check_output -Runtime standard output / standard error is output at the time of execution ・ Returns standard output when normal -In case of abnormality, subprocess.CalledProcessError exception is sent.

Execution sample


import subprocess

print '!!! start'

cmd = 'echo aaa'
d = subprocess.check_output(cmd.split())
print d,

cmd = 'unexist_command'
try:
    d = subprocess.check_output(
            cmd.split(),
            stderr=subprocess.STDOUT,
            shell=True)
    print d,
except subprocess.CalledProcessError as e:
    print e.returncode
    print e.cmd
    print e.output,

print '!!! end'

output


!!! start
aaa
127
['unexist_command']
/bin/sh: unexist_command: command not found
!!! end

Get standard error when using subprocess.check_output

Specify stderr = subprocess.STDOUT as shown below.

Example of use


    d = subprocess.check_output(
            cmd.split(),
            stderr=subprocess.STDOUT,
            shell=True)

Change the execution directory of subprocess

Using the cwd option

Specifying the execution directory


subprocess.call(cmd, cwd=workdir)

An execution example is shown below.

Execution sample


import subprocess

d1 = subprocess.check_output(['pwd'])                   #Initial position/tmp
d2 = subprocess.check_output(['pwd'], cwd='/tmp/hoge')  #Confirmation with absolute path
d3 = subprocess.check_output(['pwd'])                   #Not affected by other movements
d4 = subprocess.check_output(['pwd'], cwd='./hoge')     #Confirmation with relative path

print 'case 1:'
print d1,
print d2,

print 'case 2:'
print d3,
print d4,

Execution example


case 1:
/tmp
/tmp/hoge
case 2:
/tmp
/tmp/hoge

python subprocess changing directory - Stack Overflow

important point

When fetching an execution command from the outside, especially when shell = True is specified, a command injection attack may occur, so be careful about security.

Supplement: Command Injection Attack / Shell Injection

IPA ISEC Secure Programming Course: Web Application Chapter 6 Input Countermeasures: Command Injection Attack Countermeasures Code injection - Wikipedia

Unknown behavior

-When shell = True is specified in check_output, standard output / standard error may not be obtained. (Empty string is returned)

reference

Use communicate () when receiving output in Python subprocess-Qiita To execute a command in python-with a mug!

Recommended Posts

[Python2.7] Summary of how to use subprocess
[Python] Summary of how to use pandas
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
Summary of how to use MNIST in Python
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
[Question] How to use plot_surface of python
[Python] Summary of how to use split and join functions
[Python] How to use two types of type ()
python3: How to use bottle (2)
[Python] How to use list 1
Python: How to use pydub
[Python] How to use checkio
Summary of studying Python to use AWS Lambda
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
I tried to summarize how to use matplotlib of python
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] Summary of how to specify the color of the figure
Python: How to use async with
[Python] How to use Pandas Series
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[python] Summary of how to retrieve lists and dictionary elements
Comparison of how to use higher-order functions in Python 2 and 3
[Introduction to Python] How to use class in Python?
scikit-learn How to use summary (machine learning)
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
How to calculate Use% of df command
[Python] How to use import sys sys.argv
Memorandum on how to use gremlin python
python: How to use locals () and globals ()
How to use __slots__ in Python class
Jupyter Notebook Basics of how to use
How to use "deque" for Python data
Basics of PyTorch (1) -How to use Tensor-
How to use Python zip and enumerate
How to use regular expressions in Python
How to use is and == in Python
[Blender x Python] How to use modifiers
Summary of how to write AWS Lambda
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match