How to open new pseudo-terminal pair using Python?

PythonServer Side ProgrammingProgramming

You can use the os.openpty() to open a new pseudo-terminal pair using Python. This method gives a pair of file descriptors (master, slave), for the master and the slave end, respectively.

Example

You can use it in the following way:

import os
# master for pty, slave for tty
m,s = os.openpty()
print m
print s
# showing terminal name
s = os.ttyname(s)
print m
print s

Output

You will get the output:

3
4
3
/dev/pty0
raja
Published on 27-Dec-2017 16:01:42
Advertisements