termios --- POSIX スタイルの端末制御¶
このモジュールでは端末 I/O 制御のための POSIX 準拠の関数呼び出しインターフェースを提供します。これら呼び出しのための完全な記述については、 Unix マニュアルページの termios(3) を参照してください。これは POSIX termios 形式の端末制御をサポートしていてインストール時に有効にした Unix のバージョンでのみ利用可能です。
Availability: Unix.
このモジュールの関数は全て、ファイル記述子 fd を最初の引数としてとります。この値は、 sys.stdin.fileno() が返すような整数のファイル記述子でも、 sys.stdin 自体のような file object でもかまいません。
このモジュールではまた、モジュールで提供されている関数を使う上で必要となる全ての定数を定義しています; これらの定数は C の対応する関数と同じ名前を持っています。これらの端末制御インターフェースを利用する上でのさらなる情報については、あなたのシステムのドキュメンテーションを参考にしてください。
このモジュールには、以下の関数が定義されています:
- termios.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 indicesVMINandVTIME, 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 thetermiosmodule.
- termios.tcsetattr(fd, when, attributes)¶
Set the tty attributes for file descriptor fd from the attributes, which is a list like the one returned by
tcgetattr(). The when argument determines when the attributes are changed:- termios.TCSANOW¶
Change attributes immediately.
- termios.TCSADRAIN¶
Change attributes after transmitting all queued output.
- termios.TCSAFLUSH¶
Change attributes after transmitting all queued output and discarding all queued input.
- termios.tcsendbreak(fd, duration)¶
ファイル記述子 fd にブレークを送信します。duration をゼロにすると、0.25〜0.5 秒間のブレークを送信します; duration の値がゼロでない場合、その意味はシステム依存です。
- termios.tcdrain(fd)¶
ファイル記述子 fd に書き込まれた全ての出力が転送されるまで待ちます。
- termios.tcflush(fd, queue)¶
ファイル記述子 fd にキューされたデータを無視します。どのキューかは queue セレクタで指定します:
TCIFLUSHは入力キュー、TCOFLUSHは出力キュー、TCIOFLUSHは両方のキューです。
- termios.tcflow(fd, action)¶
ファイル記述子 fd の入力または出力をサスペンドしたりレジュームしたりします。引数 action は出力をサスペンドする
TCOOFF、出力をレジュームするTCOON、入力をサスペンドするTCIOFF、入力をレジュームするTCIONをとることができます。
- termios.tcgetwinsize(fd)¶
Return a tuple
(ws_row, ws_col)containing the tty window size for file descriptor fd. Requirestermios.TIOCGWINSZortermios.TIOCGSIZE.Added in version 3.11.
- termios.tcsetwinsize(fd, winsize)¶
Set the tty window size for file descriptor fd from winsize, which is a two-item tuple
(ws_row, ws_col)like the one returned bytcgetwinsize(). Requires at least one of the pairs (termios.TIOCGWINSZ,termios.TIOCSWINSZ); (termios.TIOCGSIZE,termios.TIOCSSIZE) to be defined.Added in version 3.11.
参考
ttyモジュール一般的な端末制御操作のための便利な関数。
使用例¶
以下はエコーバックを切った状態でパスワード入力を促す関数です。ユーザの入力に関わらず以前の端末属性を正確に回復するために、二つの tcgetattr() と try ... finally 文によるテクニックが使われています:
def getpass(prompt="Password: "):
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 = input(prompt)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
return passwd