Pexpect in Python

Pexpect in Python

  1. Pexpect in Python
  2. Methods of Pexpect in Python

We will introduce Pexpect in Python with examples.

Pexpect in Python

Python is a very popular language, and it is used for data science and machine learning. It is a very strong language because Python has built-in libraries that can be used for different purposes.

In this article, we will study the pexpect in Python. Pexpect is a powerful library used to create child processes and automatically control them.

It can test out exploits or perform any task we can think of.

Mostly it can be used to test a use case for the system to check how it works under certain tasks and how well the system performs under those tasks. We can also use it to automate some small tasks part of our daily lives.

We can use it to organize setup scripts for downloading software packages on multiple servers. Pexpect is a Python interface to the Expect family of interactive, multithreaded command-line interpreters.

Pexpect is a library for spawning networked programs on a remote machine over a network connection.

It is commonly used for scripting network services, such as SSH or Telnet. It is also useful for stand-alone spawning programs.

Now let’s install the pexpect in the software we are working on. The easiest way to install pexpect in Python is with the pip package manager.

We can install it with the following command.

# python
pip install pexpect

If you don’t have pip installed, you can install it with the following command.

# python
pip install python-pip

Methods of Pexpect in Python

Now, let’s discuss how we can use pexpect in our applications. We can use pexpect in our application with three different methods.

The first method we will discuss is the run() method.

the Run() Method in Pexpect

Run() is a powerful method of pexpect that can be used to execute a command and return the result received from that command. This method replaces another powerful library method, the os.system.

Now, let’s use this method to print Hello World using the echo command. First of all, we will import the pexpect library, and after that, we will use the run() method as shown below.

# python
import pexpect

print(pexpect.run('echo Hello World'))

Output:

pexpect run method on linux system

As you can see from the above example, our method executed the command line code inside our Python file using the run() method of pexpect.

One important thing to note from the above example is that this command will work on a Linux system, not on Windows.

the Spawn() Method in Pexpect

Now we will discuss another method of pexpect that can be used to spawn child processes and control them. As we replaced the string inside the run() method with a command line prompt, we can do the same in spawn() and use it to perform a shell command and get the result after it is executed.

Now, let’s use it in our example in which we will start a child process by using the spawn() method. We will use the same scenario to echo the string using spawn.

After that, we will send an array to the expect() method to match the above command from the strings in an array and return the element’s position that matched our command, as shown below.

# python
import pexpect

childProcess = pexpect.spawn("echo Hello World")

# prints he matched index of string.
print(childProcess.expect(["Python", "Hello World", "Child Process"]))

Output:

pexpect spawn and expect method on linux system

As you can see from the above example, the string’s position in the array was second, and the array starts from 0, so it outputs the correct position.

In this article, we discussed three methods of pexpect that can be used to perform different functions, and they can be used together to make it into a big function, as in the last example.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn