Python Glossary Русский

all Python 3Linux · macOSosfunction

os.openpty

os — pseudo-terminals (pty)

Open a new pseudo-terminal pair; returns (master, slave) fds. Availability: Unix.

Syntax

os.openpty(...)

It returns a pair: the program reads and writes the slave end believing it is a terminal, while the controlling side holds the master. Terminal emulators and automation tools work this way. Both descriptors are non-inheritable by default; the pty module is a convenient wrapper.

Examples

import os
master, slave = os.openpty()  # → ? только Unix: пара дескрипторов псевдотерминала, например (3, 4)
print(os.isatty(slave))  # → True — для программы slave-конец выглядит настоящим терминалом
print(os.ttyname(slave))  # → ? только Unix: путь slave-устройства, например /dev/pts/3
os.write(slave, b'hello\n')  # → 6 — «программа» напечатала строку в свой терминал
print(os.read(master, 1024))  # → b'hello\r\n' — драйвер pty заменил \n на \r\n

See also

Open in the full glossarydocs.python.orgReport an inaccuracy