blob: b8a3897ab8f76dc5e22087ca749ec846fabd98b5 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pty` --- Pseudo-terminal utilities
2========================================
3
4.. module:: pty
Benjamin Petersona0dfa822009-11-13 02:25:08 +00005 :platform: Linux
6 :synopsis: Pseudo-Terminal Handling for Linux.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Steen Lumholt
8.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
9
10
11The :mod:`pty` module defines operations for handling the pseudo-terminal
12concept: starting another process and being able to write to and read from its
13controlling terminal programmatically.
14
Mark Dickinson934896d2009-02-21 20:59:32 +000015Because pseudo-terminal handling is highly platform dependent, there is code to
Benjamin Petersona0dfa822009-11-13 02:25:08 +000016do it only for Linux. (The Linux code is supposed to work on other platforms,
17but hasn't been tested yet.)
Georg Brandl116aa622007-08-15 14:28:22 +000018
19The :mod:`pty` module defines the following functions:
20
21
22.. function:: fork()
23
24 Fork. Connect the child's controlling terminal to a pseudo-terminal. Return
25 value is ``(pid, fd)``. Note that the child gets *pid* 0, and the *fd* is
26 *invalid*. The parent's return value is the *pid* of the child, and *fd* is a
27 file descriptor connected to the child's controlling terminal (and also to the
28 child's standard input and output).
29
30
31.. function:: openpty()
32
33 Open a new pseudo-terminal pair, using :func:`os.openpty` if possible, or
Benjamin Petersona0dfa822009-11-13 02:25:08 +000034 emulation code for generic Unix systems. Return a pair of file descriptors
35 ``(master, slave)``, for the master and the slave end, respectively.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37
38.. function:: spawn(argv[, master_read[, stdin_read]])
39
40 Spawn a process, and connect its controlling terminal with the current
41 process's standard io. This is often used to baffle programs which insist on
42 reading from the controlling terminal.
43
44 The functions *master_read* and *stdin_read* should be functions which read from
Georg Brandl9afde1c2007-11-01 20:32:30 +000045 a file descriptor. The defaults try to read 1024 bytes each time they are
Georg Brandl116aa622007-08-15 14:28:22 +000046 called.
47
Gregory P. Smith0f21adf72012-09-29 12:41:03 -070048 .. versionchanged:: 3.4
49 :func:`spawn` now returns the status value from :func:`os.waitpid`
50 on the child process.
Georg Brandl4cf83f42010-12-30 17:22:33 +000051
52Example
53-------
54
55.. sectionauthor:: Steen Lumholt
56
57The following program acts like the Unix command :manpage:`script(1)`, using a
58pseudo-terminal to record all input and output of a terminal session in a
59"typescript". ::
60
Berker Peksagc2dd6802015-05-12 17:25:06 +030061 import argparse
62 import os
63 import pty
64 import sys
65 import time
Georg Brandl4cf83f42010-12-30 17:22:33 +000066
Berker Peksagc2dd6802015-05-12 17:25:06 +030067 parser = argparse.ArgumentParser()
68 parser.add_argument('-a', dest='append', action='store_true')
69 parser.add_argument('-p', dest='use_python', action='store_true')
70 parser.add_argument('filename', nargs='?', default='typescript')
71 options = parser.parse_args()
Georg Brandl4cf83f42010-12-30 17:22:33 +000072
Berker Peksagc2dd6802015-05-12 17:25:06 +030073 shell = sys.executable if options.use_python else os.environ.get('SHELL', 'sh')
74 filename = options.filename
75 mode = 'ab' if options.append else 'wb'
Georg Brandl4cf83f42010-12-30 17:22:33 +000076
Berker Peksagc2dd6802015-05-12 17:25:06 +030077 with open(filename, mode) as script:
78 def read(fd):
79 data = os.read(fd, 1024)
80 script.write(data)
81 return data
Georg Brandl4cf83f42010-12-30 17:22:33 +000082
Berker Peksagc2dd6802015-05-12 17:25:06 +030083 print('Script started, file is', filename)
84 script.write(('Script started on %s\n' % time.asctime()).encode())
Georg Brandl4cf83f42010-12-30 17:22:33 +000085
Berker Peksagc2dd6802015-05-12 17:25:06 +030086 pty.spawn(shell, read)
Georg Brandl4cf83f42010-12-30 17:22:33 +000087
Berker Peksagc2dd6802015-05-12 17:25:06 +030088 script.write(('Script done on %s\n' % time.asctime()).encode())
89 print('Script done, file is', filename)