How to open new pseudo-terminal pair using Python?


In the realm of Python programming, there arise times and scenarios where we need to interact and work with a terminal environment programmatically. Whether it is required to automate terminal-based tasks or to simulate user input, having the ability and skill to successfully create a new pseudo-terminal pair can be incredibly useful. In this article, we will explore different approaches of how to open a new pseudo-terminal pair using Python and the pty module. So let us get started right away. We have included some code examples with easy-to-follow explanations to guide you through the process.

Open a New Pseudo-terminal Pair

Example

We begin by importing the `pty` module; this module provides functions for working with pseudo-terminals. The `openpty()` function from the pty module is used to open a new pseudo-terminal pair. It gives as output two file descriptors: `master` and `slave`. The `master` file descriptor points to the terminal connected to the parent process, while the `slave` file descriptor indicates the terminal connected to the child process.

In this current example, we print the file descriptors of the master and slave terminals using the `print()` function. These file descriptors, it will be found, will be useful for performing operations on the pseudo-terminals; like reading from and writing to them. Then you go on to print the file descriptors of the master and slave.

Example

import pty
master, slave = pty.openpty()
print("Master:", master)
print("Slave:", slave)

Output

Master: 3
Slave: 4

Simulating User Input

Example

Once we have created the pseudo-terminal pair, we simulate or replicate user input and capture the output. In this example, we make use of the os module to work with the terminals. We go on to write the input 'Hello, terminal!\n' to the slave terminal using the os.write() function. Then, we proceed to read the output from the master terminal using the os.read() function. The decode() method is deployed to transform the output from bytes to a string. Lastly, we print the output to the console.

import os

# Write input to the slave terminal
os.write(slave, b'Hello, terminal!\n')

# Read output from the master terminal
output = os.read(master, 100)

# Print the output
print(output.decode())

Output

Hello, terminal!

Running a Command in the Pseudo-Terminal

Example

It must be noted that another powerful aspect of pseudo-terminals is the ability to run commands simulating the act of executing them in a real terminal. Here, we use the subprocess module to run and execute the command 'ls -l' in the pseudo-terminal. The check_output() function is utilized to capture the command's output. We go ahead and pass the slave terminal as the input to the command. At last, we print the output to the console.

import subprocess

# Run a command in the pseudo-terminal
command = 'ls -l'
output = subprocess.check_output(command, shell=True, stdin=slave)

# Print the command output
print(output.decode())

Output

total 8
drwx------ 5 root root 4096 Jul 17 06:13 drive
drwxr-xr-x 1 root root 4096 Jul 13 13:33 sample_data

Closing the Pseudo-Terminals

Example

Once you are done using the pseudo-terminals, it is important and essential that you close them to free system resources. In this example, we make use of the os.close() function to close both the master and slave terminals.

import os
# Close the master and slave terminals
os.close(master)
os.close(slave)

Simulating or recreating terminal interactions using pseudo-terminals opens up a whole new world of possibilities in Python. While using the pty module, we can establish a new pseudo-terminal pair, interact and work with the terminals, and even execute commands as if it was like working in a real terminal environment. If you are trying to automate tasks or attempting to test terminal-based applications, mastering the art of pseudo-terminals will go a long way in greatly enhancing your Python programming skills.

In this article, we explored and discussed in detail how to open a new pseudo-terminal pair using Python and the pty module. We learned how to open a basic pseudo-terminal pair; also gained a number of skills such as setting a custom terminal size, and customizing the environment variables for the pseudo-terminal. These techniques can be incredibly useful when you need to simulate terminal interactions, automate terminal-based tasks, or test command-line applications using code. So, by now you must have unlocked a whole new level of control and versatility in your terminal-related projects.

Updated on: 20-Jul-2023

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements