Python os.openpty() Method



Python method os.openpty() is a built-in function of OS module. It opens a pseudo-terminal pair and returns a pair of non-inheritable file descriptors for the pty and tty.

The file descriptors associated with "pty" and "tty" are called as master and slave respectively.

When a program interacts with the pty, it communicates through the master file descriptor and while interacting with tty, it uses slave file descriptor.

Syntax

Following is the syntax for Python os.openpty() method −

os.openpty()

Parameters

The Python os.openpty() method does not accept any parameters.

Return Value

The Python os.openpty() method returns a pair of file descriptors i.e., master and slave.

Example

The following example shows the usage of openpty() method where we are displaying the master file descriptor and the slave terminal name.

import os

# master for pty, slave for tty
m,s = os.openpty()
print("File descriptors for pty:")
print (m)
print (s)

# showing terminal name
s = os.ttyname(s)
print("File descriptors for tty:")
print (m)
print (s)

When we run above program, it produces following result −

File descriptors for pty:
3
4
File descriptors for tty:
3
/dev/pts/1
python_files_io.htm
Advertisements