cpython/Doc/library/termios.rst

106 lines
3.7 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:01 -03:00
:mod:`termios` --- POSIX style tty control
==========================================
.. module:: termios
:platform: Unix
:synopsis: POSIX style tty control.
.. index::
pair: POSIX; I/O control
pair: tty; I/O control
This module provides an interface to the POSIX calls for tty I/O control. For a
complete description of these calls, see the POSIX or Unix manual pages. It is
only available for those Unix versions that support POSIX *termios* style tty
I/O control (and then only if configured at installation time).
All functions in this module take a file descriptor *fd* as their first
argument. This can be an integer file descriptor, such as returned by
``sys.stdin.fileno()``, or a file object, such as ``sys.stdin`` itself.
This module also defines all the constants needed to work with the functions
provided here; these have the same name as their counterparts in C. Please
refer to your system documentation for more information on using these terminal
control interfaces.
The module defines the following functions:
.. function:: tcgetattr(fd)
Return a list containing the tty attributes for file descriptor *fd*, as
follows: ``[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]`` where *cc* is a
list of the tty special characters (each a string of length 1, except the
items with indices :const:`VMIN` and :const:`VTIME`, which are integers when
these fields are defined). The interpretation of the flags and the speeds as
well as the indexing in the *cc* array must be done using the symbolic
constants defined in the :mod:`termios` module.
.. function:: tcsetattr(fd, when, attributes)
Set the tty attributes for file descriptor *fd* from the *attributes*, which is
a list like the one returned by :func:`tcgetattr`. The *when* argument
determines when the attributes are changed: :const:`TCSANOW` to change
immediately, :const:`TCSADRAIN` to change after transmitting all queued output,
or :const:`TCSAFLUSH` to change after transmitting all queued output and
discarding all queued input.
.. function:: tcsendbreak(fd, duration)
Send a break on file descriptor *fd*. A zero *duration* sends a break for 0.25
--0.5 seconds; a nonzero *duration* has a system dependent meaning.
.. function:: tcdrain(fd)
Wait until all output written to file descriptor *fd* has been transmitted.
.. function:: tcflush(fd, queue)
Discard queued data on file descriptor *fd*. The *queue* selector specifies
which queue: :const:`TCIFLUSH` for the input queue, :const:`TCOFLUSH` for the
output queue, or :const:`TCIOFLUSH` for both queues.
.. function:: tcflow(fd, action)
Suspend or resume input or output on file descriptor *fd*. The *action*
argument can be :const:`TCOOFF` to suspend output, :const:`TCOON` to restart
output, :const:`TCIOFF` to suspend input, or :const:`TCION` to restart input.
.. seealso::
Module :mod:`tty`
Convenience functions for common terminal control operations.
Example
-------
.. _termios-example:
Here's a function that prompts for a password with echoing turned off. Note the
technique using a separate :func:`tcgetattr` call and a :keyword:`try` ...
:keyword:`finally` statement to ensure that the old tty attributes are restored
exactly no matter what happens::
Merged revisions 74861-74863,74876,74896,74930,74933,74952-74953,75015,75019,75260-75263,75265-75266,75289 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74861 | benjamin.peterson | 2009-09-17 05:18:28 +0200 (Do, 17 Sep 2009) | 1 line pep 8 defaults ........ r74862 | brett.cannon | 2009-09-17 05:24:45 +0200 (Do, 17 Sep 2009) | 1 line Note in the intro to Extending... that ctypes can be a simpler, more portable solution than custom C code. ........ r74863 | benjamin.peterson | 2009-09-17 05:27:33 +0200 (Do, 17 Sep 2009) | 1 line rationalize a bit ........ r74876 | georg.brandl | 2009-09-17 18:15:53 +0200 (Do, 17 Sep 2009) | 1 line #6932: remove paragraph that advises relying on __del__ being called. ........ r74896 | georg.brandl | 2009-09-18 09:22:41 +0200 (Fr, 18 Sep 2009) | 1 line #6936: for interactive use, quit() is just fine. ........ r74930 | georg.brandl | 2009-09-18 23:21:41 +0200 (Fr, 18 Sep 2009) | 1 line #6925: rewrite docs for locals() and vars() a bit. ........ r74933 | georg.brandl | 2009-09-18 23:35:59 +0200 (Fr, 18 Sep 2009) | 1 line #6930: clarify description about byteorder handling in UTF decoder routines. ........ r74952 | georg.brandl | 2009-09-19 12:42:34 +0200 (Sa, 19 Sep 2009) | 1 line #6946: fix duplicate index entries for datetime classes. ........ r74953 | georg.brandl | 2009-09-19 14:04:16 +0200 (Sa, 19 Sep 2009) | 1 line Fix references to threading.enumerate(). ........ r75015 | georg.brandl | 2009-09-22 12:55:08 +0200 (Di, 22 Sep 2009) | 1 line Fix encoding name. ........ r75019 | vinay.sajip | 2009-09-22 19:23:41 +0200 (Di, 22 Sep 2009) | 1 line Fixed a typo, and added sections on optimization and using arbitrary objects as messages. ........ r75260 | andrew.kuchling | 2009-10-05 23:24:20 +0200 (Mo, 05 Okt 2009) | 1 line Wording fix ........ r75261 | andrew.kuchling | 2009-10-05 23:24:35 +0200 (Mo, 05 Okt 2009) | 1 line Fix narkup ........ r75262 | andrew.kuchling | 2009-10-05 23:25:03 +0200 (Mo, 05 Okt 2009) | 1 line Document 'skip' parameter to constructor ........ r75263 | andrew.kuchling | 2009-10-05 23:25:35 +0200 (Mo, 05 Okt 2009) | 1 line Note side benefit of socket.create_connection() ........ r75265 | andrew.kuchling | 2009-10-06 00:31:11 +0200 (Di, 06 Okt 2009) | 1 line Reword sentence ........ r75266 | andrew.kuchling | 2009-10-06 00:32:48 +0200 (Di, 06 Okt 2009) | 1 line Use standard comma punctuation; reword some sentences in the docs ........ r75289 | mark.dickinson | 2009-10-08 22:02:25 +0200 (Do, 08 Okt 2009) | 2 lines Issue #7051: Clarify behaviour of 'g' and 'G'-style formatting. ........
2009-10-27 11:59:26 -03:00
def getpass(prompt="Password: "):
2007-08-15 11:28:01 -03:00
import termios, sys
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ECHO # lflags
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
passwd = raw_input(prompt)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return passwd