Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to open new pseudo-terminal pair using Python?
Pseudo-terminals (pty) are an advanced and powerful technique in Python when dealing with terminal-based processes or simulating interactive sessions programmatically. A pseudo-terminal allows a process to connect to a real terminal, enabling control over terminal input and output.
Python provides a built-in pty module that helps in creating and managing these terminal pairs. In this article, we will learn how to open a new pseudo-terminal pair using Python.
Understanding Pseudo-terminals
A pseudo-terminal consists of two parts: a master and a slave. The master end is controlled by your Python program, while the slave end behaves like a regular terminal that can be used by other processes.
Using pty.openpty()
The pty module provides the openpty() function that returns a tuple containing file descriptors for the master and slave ends of a new pseudo-terminal pair.
Syntax
master_fd, slave_fd = pty.openpty()
Example
Here's how to create a new pty pair using pty.openpty() ?
import os
import pty
# Open a new pseudo-terminal pair
master_fd, slave_fd = pty.openpty()
# Get the name of the slave device
slave_name = os.ttyname(slave_fd)
print("Master FD:", master_fd)
print("Slave FD:", slave_fd)
print("Slave device path:", slave_name)
# Clean up
os.close(master_fd)
os.close(slave_fd)
The output of the above code is ?
Master FD: 3 Slave FD: 4 Slave device path: /dev/pts/3
Using pty to Spawn a Shell
You can spawn a shell process connected to the slave end of the pseudo-terminal. This is useful for terminal emulation or controlling shell processes programmatically.
Example
Here's how to spawn a shell and interact with it through the master end ?
import os
import pty
import subprocess
import time
master_fd, slave_fd = pty.openpty()
try:
# Start a shell connected to the slave
proc = subprocess.Popen(
['/bin/bash'],
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
close_fds=True
)
# Close slave fd in parent process
os.close(slave_fd)
# Send command to shell
os.write(master_fd, b'echo "Hello from pty"\n')
time.sleep(0.1) # Give time for command to execute
# Read output
output = os.read(master_fd, 1024)
print("Shell Output:", output.decode().strip())
# Terminate the shell
os.write(master_fd, b'exit\n')
proc.wait()
finally:
os.close(master_fd)
When you run the above program, a new bash shell is started, and it prints the message from our Python script ?
Shell Output: Hello from pty
Key Points
- Use
pty.openpty()to create a low-level terminal emulation channel - Use
os.ttyname()to identify the slave terminal path - Always close file descriptors to prevent resource leaks
- Connect subprocesses to the slave end to simulate terminal sessions
- The master end is used by your Python program for reading/writing
Platform Compatibility
Note: The pty module is available only on Unix-like systems (Linux, macOS) and is not supported on Windows. For Windows pseudo-terminal functionality, you can use the pywinpty library on Windows 10+.
Conclusion
Opening a pseudo-terminal pair in Python provides powerful capabilities for terminal emulation and process control. Use pty.openpty() for basic pty creation and combine it with subprocess management for advanced terminal-based applications.
