Learn about the subprocess library in Python - ProgrammerTech
Language:

Search

Learn about the subprocess library in Python

  • Share this:
Learn about the subprocess library in Python

subprocess library in python

The subprocess library is a library we have in Python that enables us to deal with the command line to see stdout and stderr and it is very, very important and we will deal with it in security and in networks.

 

Show subprocess results and errors

We will now use a function that allows us to show results, errors, if any, and inputs in more detail.

Example 1

For example, we create a Python file on the desktop with the name subprocess_.py and write inside it

from subprocess import PIPE , Popen
p = Popen  ("tasklist",stdout=PIPE,stderr=PIPE,shell=True)
print(p.communicate()[0])

We said in the example that we have a process and we used Popen with it and he took some argument from us, the first of which is the tasklist that we have within the system, and the second is stdout, and the third is the stderr error if there is an error and the shell is True, and this is one of the basics in the process, and then to see The result is that we use p.communicate. The tuple will only return two objects, the first stdout, and the second stderr.

Run it with cmd now to see the result.

1-60.webp

 

But if we use a command that we do not have in the system as follows:

from subprocess import PIPE , Popen
p = Popen  ("tasklist",stdout=PIPE,stderr=PIPE,shell=True)
print(p.communicate()[0])

When we run, it will not display anything because the command does not exist, and if we want to display the message does not exist, we use change instead of the 0 we put it 1 to display the errors as follows.

from subprocess import PIPE , Popen
p = Popen  ("lskd",stdout=PIPE,stderr=PIPE,shell=True)
print(p.communicate()[1])

It will give us the following result.

2-53.webp

 

If we want the result to be displayed regularly, we use the following in our file

from subprocess import PIPE , Popen
p = Popen  ("tasklist",stdout=PIPE,stderr=PIPE,shell=True)
print(p.communicate()[0].decode("utf-8"))

The result will be the same, but in an orderly fashion.

Example 2

process_1 = subprocess.Popen(['ls','-al'],stdin = subprocess.PIPE,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE, shell = True)
output , error = process_1.communicate()
print(output)
print(error)

And the outputs will be as follows:

b'_test_runner.py\nmain.py\nout.txt\n'
b''

Each process must have inputs, outputs and errors, if they exist. In order to list the outputs and errors, we have added the following:

stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE

In order to activate the process of enumeration from within the function, the communicate function sorts the outputs and errors into variables to benefit from and display them:

output , error = process_1.communicate()
print(output)
print(error)

Let's take what we've learned and direct our output to a file.

import subprocess
fout = open('out.txt','w+')
process_1 = subprocess.Popen(['ls','-al'],stdin = subprocess.PIPE,
import subprocess
fout = open('out.txt','w+')
process_1 = subprocess.Popen(['ls','-al'],stdin = subprocess.PIPE,
    stdout = fout,
    stderr = subprocess.PIPE, shell = True)
output , error = process_1.communicate()
print(output)
print(error
    stdout = fout,
    stderr = subprocess.PIPE, shell = True)
output , error = process_1.communicate()
print(output)
print(error)

We also note that after we have created a file and its status is read and write, and we direct the output from the command to the file:

stdout = fout

And we open the file out.txt and display its contents. We will find that the contents of the output have been directed and written in it:

_test_runner.py
main.py
out.txt

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com